An Introduction to Session

1. Definition edit

Session is quite difficult to translate directly into Chinese, so it is generally translated as 时域 (time domain). In computer terminology, Session refers to the time interval during which a terminal user communicates with an interactive system, usually the time from logging into the system to logging out of the system. And if necessary, there may also be a certain amount of operating space.

Specifically for the Web, Session refers to the period of time from when a user enters a website to when that website is closed while browsing that website, that is, the time the user spends browsing the site. So from the definition above we can see that Session is actually a specific time concept.

It should be noted that the concept of a Session needs to include a specific client, a specific server side, and an uninterrupted period of operation time. The Session when user A establishes a connection with server C is a different Session from the one when user B establishes a connection with server C.

How session works

(1) When a session is first enabled, a unique identifier is stored in the local cookie.

(2) First the session_start() function is used, and PHP loads the already-stored session variables from the session repository.

(3) When the PHP script is executed, the session variables are registered using the session_register() function.

(4) When the PHP script finishes executing, the session variables that have not been destroyed are automatically saved to the session library under a certain local path. This path can be specified via session.save_path in the php.ini file, and can be loaded and used the next time you browse a web page.

2. Solution edit

So what is the Session solution? As we know, users often need to browse many web pages when visiting a website. The use of Session differs in its usage and characteristics across different languages. For a website built with PHP, the user needs to execute many PHP scripts during their visit. However, due to the characteristics of the HTTP protocol itself, each time a user executes a PHP script they need to re-establish a connection with the web server.

And because of its stateless nature, this connection cannot obtain the state of the previous connection. Thus, if a user assigns a value to a variable in one PHP script, that variable’s value cannot be obtained in another PHP script. For example, if the user sets $user=”wind” in the PHP script responsible for login, they cannot obtain the value “wind” by calling $user in another PHP script. In other words, global variables cannot be set in PHP. The variables defined in each PHP script are local variables that are only valid within that script.

The Session solution is meant to provide a way to define global variables in PHP scripts, so that such a global variable is valid for all PHP scripts within the same Session. As we mentioned above, a Session is not a simple time concept; a Session also includes a specific user and server. So, more precisely, the scope of a global variable defined in a Session refers to all the PHP accessed by the user corresponding to that Session.

For example, user A defines a global variable $user=“wind” through a Session, while user B defines the global variable $user=“jane” through a Session. Then in the PHP scripts accessed by user A, the value of $user is wind.

3. How to use it edit

Session is a web server-based method for maintaining state. Session allows any object to be maintained throughout the user session by storing it in the memory of the web server.

Session is typically used to do the following

Store information that needs to maintain its state throughout the user session, such as login information or other information the user needs while browsing a web application.

Store objects whose state only needs to be maintained across page reloads or between a group of pages grouped by function.

What Session does is maintain the user’s state information on the web server so it can be accessed from any page at any time. Because the browser does not need to store any of this information, any browser can be used, even browser devices like pads or phones.

Limitations of the persistence approach

As more and more users log in, the amount of server memory required by Session also keeps increasing.

Each user accessing the web application generates a separate Session object. The duration of each Session object is the user’s access time plus the inactivity time.

If many objects are maintained in each Session, and many users use the web application simultaneously (creating many Sessions), the amount of server memory used for Session persistence may become very large, which affects scalability.

4. Understanding edit

  1. For value type variables, Session stores a copy of the value type

    Session\["__test0"\] = 1;
    
    int i = (int)Session\["__test0"\]+1;
    
    int j = (int)Session\["__test0"\];
    

    Result: i=2,j=1

  2. For reference type variables, Session stores the reference

    CDACommon cda = new CDACommon();
    
    Session\["\_\_test"\] = cda.GetDataSet("select top 1 * from tb\_customer");
    
    DataSet ds = (DataSet)Session\["__test"\];
    
    DataSet ds2 = (DataSet)Session\["__test"\];
    
    ds.Tables\[0\].Rows\[0\]\[0\]="9999";
    

    Result:

    ds.Tables\[0\].Rows\[0\]\[0\]=="9999"
    
    ds2.Tables\[0\].Rows\[0\]\[0\]=="9999";
    
  3. Session lifecycle

    After a new browser window is launched, a new Session begins, triggering the call to Global’s Session_Start. Browser windows opened from the first browser window do not start a new Session. After a Session expires, submitting a page also triggers Session_Start, which amounts to a new Session.

  4. Calling Session

    For a Web Service, each method call starts a Session. You can use the following approach to make multiple calls be in the same Session  CWSSyscfg cwsCfg = new CWSSyscfg(); cwsCfg.CookieContainer = new System Net.CookieContainer(); CWSSyscfg is a Web Service class. By setting the CookieContainer property on the Web Service’s proxy classes, as long as the CookieContainer property of multiple proxies has the same value, the calls to these Web Services will be in the same Session. This can be implemented using the singleton pattern.

  5. Session data validity period

    As long as the page has submission activity, all items in the Session are maintained. When the page has no submission activity for 20 minutes (the default configuration), the Session becomes invalid. The multiple data items stored in a Session become invalid as a whole.

  6. Saving Session

    If a non-serializable class such as DataView is saved in a Session, it cannot be used in the mode where SQLServer is used to save the Session. To check whether a class is serializable, you need to see whether the class is marked with [Serializable].

In PHP

PHP session variables are used to store information about a user session, or to change the settings of a user session. Session variables store information about a single user and are available to all pages in the application.[1]

PHP Session variables

When you operate an application on your computer, you open it, make some changes, and then close it. This is much like a conversation (Session). The computer knows who you are. It knows when you open and close the application. However, a problem arises on the Internet: because HTTP addresses cannot maintain state, the web server does not know who you are or what you did.

PHP session solves this problem by storing user information on the server for later use (such as user name, purchased items, etc.). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in a database.

How Session works is: a unique id (UID) is created for each visitor, and variables are stored based on this UID. The UID is stored in a cookie or transmitted via the URL.

————Excerpted from Baidu