PHP session processing function, session
PHP Session variable
When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). 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 the database.
The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL.
1. Start session
Before storing information to a session, you must enable the session. Php provides the session_start () function to start or continue a session. Definition:
1 bool session_start (void)
The call is as follows:
<?php session_start();?>
Note:
(1) The session_start () function must be located before the
(2) Whether a session is successfully created or not, the session_start () function returns TRUE, so any exception processing does not work.
(3) You can also enable the configuration command session. auto_start so that you do not need to execute this function. However, each php page starts or continues a session during execution.
2. Store or read sessions
The correct method for storing and reading session variables is to use the s ession variable of php. SESSION variable. _ SESSION is a global parameter provided by php to store and read sessions. (Note that the key of the associated array is consistent with the naming rules of common variables)
You can assign values to a stored session directly.
$ _ SESSION ['season '] = 'autumn ';
The preceding example sets a session element with the key name "season" and its value is "Autumn ". When reading, it is like calling a normal array element.
The following two sections of Code show how to store and read a session element.
This is the session1.php file:
<? Php/*** Created by PhpStorm. * User: yuxiu * Date: // * Time: */if (isset ($ _ POST ['submit ']) {session_start (); // start to create a SESSION $ _ session ['season '] = $ _ POST ['season']; // store the SESSION data header ("Location: session. php "); // special attention should be paid to the format problem in header ()}?> <B> storage session </B>
This is the session2.php file:
<? Php/*** Created by PhpStorm. * User: yuxiu * Date: // * Time: */session_start (); // create or continue a SESSION $ season =$ _ SESSION ['season ']; // read session data echo "<B> Read session </B> <br/>"; switch ($ season) {case 'spring '; echo 'It's green spring now! '; Break; case 'summer'; echo 'is a hot summer now! '; Break; case 'loan'; echo' is now the autumn of the harvest fruit! '; Break; case 'Winter'; echo 'is now a snowy winter! '; Break; default; echo' sorry, the session has no data or does not exist! ';}?>
In session1.php, you first use session_start () to create a session, and then store the submitted seasonal data by assigning values to the array. Finally, you can use the header () function to directly jump to the start. In the session2.php file, you also need the session_start () function to continue a session and call the session information using the session array.
3. Destroy sessions
When a session is no longer used, it needs to be manually destroyed. Although php has the function of automatically destroying the session, this will reduce the efficiency of the program. You can use the unset () function or session_destroy () function.
<?php unset($_SESSION['season']);?>
Or:
<? Php session_destroy (); // note that using this function will reset the session array to lose all the stored session data?>