PHP session variable is to store the user's session information, or change the user's session settings. The session variable stores a single user's information, which can be used by all pages.
PHP Session Variable
When you run an application on your computer, you open it, make some changes to him, and then close it, which is similar to the session. The computer knows who you are and when you start the application and when it shuts down. But on the internet, there is a problem: Because HTTP addresses cannot be permanently preserved, it is difficult for the server to identify who you are and what you are doing.
The PHP session allows you to store user information (such as user name [username], shopping list [shopping], etc.) on the server to solve the problem. However, the session information is also temporary, and when you leave the site, he will be automatically deleted. If you want to keep this information permanently, you can try to store it in the database.
The session runs by creating a separate ID (UID) for each visitor and storing the UID based variable. The UID is stored in a cookie and is displayed in the URL.
Start the PHP session
You must start the session before you put the user information into PHP session.
Note: the Session_Start () function must be written before the
<?php session_start(); ?>
<body></body>
The code above will register a user's session on the server, allow you to store user information, and specify a UID for the user sessions.
Store a Session variable
The best way to store and get session variables is to use PHP $_session variables:
<?php
session_start();
// store session data
$_SESSION['views']=1;
?> <body><?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?></body>
Results:
Pageviews=1
In the above case, we set up a simple page counter. The Isset () function checks whether the "views" variable has been set. If the "views" variable has been set, we will increase our count. If the "views" variable does not exist, we will first create a "views" variable and assign "1" to it.
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;echo "Views=". $_SESSION['views'];
?>
Delete session
If you want to delete some session data, you can use the unset () function or the Session_destroy () function.
The role of the Unset () function is to release the specified session variable:
<?phpunset($_SESSION['views']);
?>
You can also use the Session_destroy () function to remove all sessions:
<?php
session_destroy();
?>
Note: Session_destroy () will reset your session, and you will lose all the saved sessions data.