PHP Session

PHP session variables are used to store information about the user session (session), or to change the settings of the user session (session). The Session variable stores information for a single user and is available to all pages in the application.


PHP Session Variable

When you operate an application on your computer, you open it and do something Change and then close it. It's a lot like a conversation. The computer knows who you are. It knows when you open and close apps. However, a problem arises on the Internet: Since HTTP addresses cannot maintain state, the web server does not know who you are and what you have done.

PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, 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 working mechanism of Session is to create a unique id (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or passed through the URL.

Open session


##First we need to open session, so first The first function to learn is

session_start()

. This function has no parameters. Use

Comments at the beginning of the php file: The session_start() function must be located before the <html> tag:

<?php session_start (); ?> <html>

<body>

</body> </html>





Add session data


After opening the session, Then in the next processing, we can use the $_SESSION variable to access information. What we need to know is that the $_SESSION variable is an array. When we want to store information in the session, what should we write:

##

<?php
$_SESSION['userName'] = 'wang';
?>

Read session data


Reading is very simple, just like we use an array, as follows:
$userName = $_SESSION['userName'];

Of course you can also use

$_SESSION['userName']. Used in the same way as arrays.


Destroy session data

##We can use many ways to destroy session data.

a) unset function

We use something like

unset($_SESSION['XXX'] );


to destroy the XXX variable in the session.

PS: Please don’t! Please do not! Please do not unset($_SESSION), which will result in the $_SESSION variable being unable to be used later! ! !

b) Assign an empty array to the session variable

$_SESSION = array();


We said before that the $_SESSOIN variable is an array, so assigning an empty array is equivalent to destroying the value in the $_SESSION variable of the current session.

c) session_destory() function

This function will destroy all data in the current session and end the current session. However, global variables associated with the current session will not be reset, nor will session cookies be reset.


Storage Session variable

Example

The correct way to store and retrieve session variables is to use the PHP $_SESSION variable:

<?php
 session_start();
 // 存储 session 数据
 $_SESSION['views']=100;
 ?>
 
 <html>
 <head>
     <meta charset="utf-8">
     <title>php中文网(php.cn)</title>
 </head>
 <body>
 
 <?php
 // 检索 session 数据
 echo "浏览量:". $_SESSION['views'];
 ?>
 
 </body>
 </html>

Program running results:

Views: 100


##Example

In the following example, we create a simple page-view counter. The isset() function detects whether the "views" variable has been set. If the "views" variable is set, we increment the counter. If "views" does not exist,

Create the "views" variable and set it to 1, which will be incremented by 1 every time the page is refreshed

<?php
header("Content-type:text/html;charset=utf-8"); //设置编码
// 存储 session 数据
session_start();
if(isset($_SESSION['views']))
{
    $_SESSION['views']=$_SESSION['views']+1;
}
else
{
    $_SESSION['views']=1;
}
echo "浏览量:". $_SESSION['views'];
?>

Run your program to see how many views you have

Continuing Learning
||
<?php session_start(); // 存储 session 数据 $_SESSION['views']=100; ?> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <?php // 检索 session 数据 echo "浏览量:". $_SESSION['views']; ?> </body> </html>
submitReset Code