Question re Using Session Variables In Called Methods and Functions

I am having a fair amount of difficulty trying to work out how to use session variables so that I get predictable and consistent outcomes. The documentation and training is poorly constructed around this topic, multiple sources of the “truth” which are contradictory in places.

This is not a great example, but hopefully you will get the gist of my challenge. Let’s assume I have a blank application (I am trying to keep this very simple). The Execute method calls the PHP Method set_session_variable() which reads a value from the DB and sets a session variable (the_session_var) based on the DB value.

Execute:

if ( !(set_session_variable()) ) 
{

	// set failed - process an error - display message in [the_session_var] & reset it

} 
else
{
	// good set - redirect to form
}

PHP Method set_session_variable:

// logic to get variable from DB here - loads value into $my_name - use your imagination :-)

if (good_get) 
{
	if (test_some_condition_against_db_value) 
	{
		[the_session_var] = ‘x’;
	} 
	else
	{
		[the_session_var] = ‘y’;
	}
	return TRUE;
}
else
{
	[the_session_var] = ‘error message’;
	return FALSE;
}

Problem: When the Execute event redirects, the session variable is not set for the app I redirected to.

With the above in mind, there are a few things I have found, but I am not sure if I am doing things incorrectly. Am I failing to set some options properly that impact what is being generated?

In looking at the generated code, when you set a session variable (use of square brackets, e.g. [the_session_var] = ‘y’) what gets generated is code to set a SC generated temporary variable, in this case $this->sc_temp_the_session_var. The code to set the session variable is not inserted until the default (fall-through) exit point of the function - for any session variable accessed in the function there will be a bunch of statements inserted immediately prior to the default exit point, like this:

 `if (isset($this->sc_temp_the_session_var)) {$_SESSION['the_session_var'] = $this->sc_temp_the_session_var;}`	

But note that in my PHP Method, we exit before the default (fall-through) exit point of the function using return statements that return success or failure (true or false) to the calling event. So the statement immediately above that actually sets the session variable is never reached - only the temporary variable is set.

First question, does anyone understand what the scope of $this->sc_temp_the_session_var is? In other words, if I write $local_var = [the_session_var] anywhere in my app (any PHP methods, events, buttons, AJAX events, or internal libraries), will it successfully access the variable event though the session variable is not set?

Next question, how ,in SC, do you ensure a session variable is set if you want to return before the default fall-through exit point? Do I just code “$_SESSION[‘the_session_var’] = $this->sc_temp_the_session_var;” myself? Doesn’t this undermine the value of SC? It seems to me that if you allow a macro syntax like [the_session_var] = ‘x’, which supposedly sets a session variable, then it should do it.

Next, (and to be clear, I have not experimented with this yet - I’ll get to that…), this training video (https://www.youtube.com/watch?v=NBgkPlfIOPA), which was only published in May 2020, makes reference to the sc_set_global macro. Should I be using this macro to set the variable instead of the “[the_session_var] = “ syntax? Does anyone know if this forces the immediate setting of $_SESSION['the_session_var’]?

**Update**: I have now looked into some of this.  Using...
      [the_session_var] = 1;
...generates...
      $this->sc_temp_the_session_var = 1;
Using...
      $the_session_var = 1;
      sc_set_global($the_session_var);
... generates...
      $the_session_var = 1;
      if (isset($the_session_var)) {$this->sc_temp_the_session_var = the_session_var;}
Note that the session variable is not set, only the temp variable.  So, I guess, there is limited value in the sc_set_global macro, which possibly answers the following question.

Finally, you might be asking why I have not tried sc_set_global yet. Well, in the macro documentation there is this warning:

NOTE: This macro will be discontinued soon. You should use the method of creating global variables using brackets. Ex: [var_glo_user] = "test";

So, if the sc_set_global is the only way to achieve what I need, why is it being discontinued? If it is being discontinued, why is it included in video training published this year? (UPDATE: It does not achieve what we need, but it is still a good question???)

I find this all very confusing. Can anyone help clarify?

Kind regards.

Yes - I agree the documentation is painful in places and getting your head round the way the session variables work is difficult to follow.

One of the key ‘catches’ is that a session variable isn’t in relation to the app, but the event. So a session variable set in onBeforeUpdate is not available to onAfterUpdate for example.

I always use globals unless its a very well contained use in a single event.

You can just set a global variable by declaring it eg

[a_global] = 2;

and as I understand it this is now the preferred method.