PHP Session Variable
When you run an application, you open it, make some changes, and then close it. It's like a conversation. The computer knows who you are. It knows when you start the application and when it terminates. But on the internet, there is a problem: the server does not know who you are and what you do, because the HTTP address is not maintained.
The PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchase item, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
The session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.
1. Start session
Before you store the information in session, you first have to open it. PHP provides the session_start () function to start or continue a session. Defined:
1 bool session_start (void)
The call is as follows:
<?php session_start ();? >
Attention:
(1) the session_start () function must be in the
(2) the Session_Start () function returns True regardless of the successful creation of the session, so using any exception handling will not work.
(3) You can also enable configuration directive Session.auto_start so that you do not have to perform this function, but in this case, each PHP page will start or resume a session when it executes.
2. storing or reading sessions
The correct way to store and read session variables is to use the PHP s ession variable. The session variable. _session is a global parameter provided by PHP that is designed to store and read sessions. (Note that the key name of the associative array is consistent with the naming rules for ordinary variables)
When you store a session, you can assign a value directly to it.
$_session[' season '] = ' autumn ';
The above set a session element with a key named "season" with the value "fall". When read, 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 (); Begin to establish a session
$_session[' season ' = $_post[' season ']; Store 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 (); Establish or continue a session
$season = $_session[' season ']; Read session data
echo "<b> read session </b><br/><br/>";
Switch ($season) {case
' spring ';
Echo ' is now a green spring! ';
break;
Case ' summer ';
Echo ' Now is the summer of passionate overflowing! ';
break;
Case ' fall ';
Echo ' is now the harvest fruit of Autumn! ';
break;
Case ' Winter ';
Echo ' Now is the snowy winter! ';
break;
default;
Echo ' Sorry, there is no data in the session or there is no such conversation! ';
}
? >
In session1.php, you first create a session using Session_Start () and then store the submitted season data using an array assignment, and then use the header () function to jump directly to the beginning. In the session2.php file, the Session_Start () function is also required to continue a session and invoke session information using the sessions array.
3. Destruction of sessions
When the session is no longer in use, it is necessary to artificially destroy it, although PHP has the ability to automatically destroy the session, but this will make the program less efficient. You can use the unset () function or the Session_destroy () function.
<?php
unset ($_session[' season '));
? >
Or:
<?php Session_destroy (); Note that using this function resets the session array, which loses all of the stored session data.