The PHP session variable is used to store information about a user's session or to change settings for a user session. The Session variable holds information that is single-user and can be used by all pages in the application.
PHP Session variables
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 does not maintain state.
By storing user information on the server for later use ( such as paging, storing the page's information, the total number of pages, total pages, etc. ), the PHP session solves the problem (such as user name, purchase of goods, etc.). However, the session information is temporary and will be deleted when 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.
Start PHP Session
You must use the Session_Start () function to start the session before you use the session.
Server-side:
<? PHP Session_Start (); // store session data$_session[' views ']=1;? >
Front:
PHP // Retrieve session Data Echo $_session [' views '];? ></body>
The front-end output is:
Pageviews=1
The following is a simple Page-view counter. The Isset () function detects whether the "views" variable has been set. If you have set the "views" variable, we accumulate the counter. If "views" does not exist, then we create "views" variable and set it to 1:
<? PHP Session_Start (); if (isset($_session[' views '])) $_session [' Views ' of]=$_session[' views ']+1; Else $_session [' Views ']=1; Echo $_session [' views '];? >
End Session
If you want to delete some session data, you can use the unset () or Session_destroy () function.
You can use the unset () function to release the specified session variable:
<? PHP unset ($_session[' views ']);? >
You can also completely terminate the session with the Session_destroy () function, and all session data will disappear.
<? PHP Session_destroy ();? >
PHP Super global variable--session variable