Difference between global and session variables

Hi
i was wondering about differences between global and session variables
in SC doc it is not really clear what are the consequences/real difference.
any security differences or other ?

1 Like

Global variables are only valid within the scope of the module you are running, not outside. Session variables are bound to a session ($_SESSION) which means that they are shared over your modules. Then you have get and post which you should avoid due to security issues. Again, a long time discussion with SC, as I believe that session should be default.

1 Like

I was always assuming global variables are also stored in the session on the server , where are the values are stored if not in the session?
could you elaborate what you call modules?
i can use a global variable across apps, i assume you mean by modules something else?
thank Albert

I guess that all are stored in the session, you can verify it printing the $_SESSION variable, if I recall correctly global variables are stored as _SESSION['scriptcaseì][‘globalvar’], while module variables are stored as _SESSION['scriptcaseì]['modulenameì][‘variablname’], or something like that, I don’t check those things since months.

1 Like

I have to disagree partially on what Albert says.

A Scriptcase global variable [myvar] is saved into PHP session and is accessible to ALL modules (apps) sharing the same session.
As soon as a global variable is assigned (regardless is declared as GET,POST or SESSION, and this is another big source of confusion) it is saved into the PHP session.
A module variable (called attribute in SC language {myvar}) is also saved into PHP session but is kept separate among modules (e.g. you can have two modules using the same name for an attribute and the corresponding values are kept separate).

Keep in mind that global and module variables, being saved in the PHP session, are accessible to all PHP scripts sharing the same session, regardless are written in SC or not, using $_SESSION array.

$_SESSION[‘myvar’] and SC global [myvar] and SC attribute {myvar} are all saved in PHP session but in different places so the three values are kept separate.

Due to the way PHP sessions work, if you don’t want that a SC global variable is shared among different apps you need to make sure that each app runs in a separate session (e.g. apps need to be accessed with different host names)

4 Likes

Thank you. I can’t always be right.

4 Likes

Come on Albert you are a star in this forum !

5 Likes

I was wondering if attributes can be used in the queries as well or be pathed through on opening of an app? otherwise besides sharing variables among the events I do not see a practical value .
If attributes could stick to each open instance of an app that would be great. right now it is a problem to open multiple instances of the same app with different filters if a global variable used in the query where.
it would indeed more useful if global variables could stick to each open instance of the app, and session variables be true global among the apps.

image

  1. You can use attributes in an SQL query by using the sc_select_where(add) macro in the OnScriptInit event.

  2. The practical advantage of attributes is that they are local to the app and do not get mixed up across different apps like GlobalVars do.

  3. Global variables and attributes are ultimately saved in the PHP session. Therefore, if you need to open several instances of the same app, one way to keep the sessions separate is to use different URLs. For example: http://myserver1/MyApp.php, http://myserver2/MyApp.php, and http://myserver3/MyApp.php will open three instances of MyApp, and each instance will remain well-separated from the others.

1 Like