PHPsession session processing function, session. PHPsession session handler. when you run an application, you can open it, make some changes, and close it. This is like a session. Computer 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 locatedBefore the tag, that is, the function must be called before any output. when you write a program, you often enter a space or press enter, and an error is reported. Special attention should be paid to this. (I was pitted)
(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 ()}?>Storage session
Select the data to be set:
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"Read session
"; Switch ($ season) {case 'spring'; echo 'is 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?>
Http://www.bkjia.com/PHPjc/1133847.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133847.htmlTechArticlePHP session handler, session Session PHP session variable when you run an application, you will open it, make some changes, and then close it. This is like a session. Computer...