Home  >  Article  >  Backend Development  >  PHP Session simple application routine

PHP Session simple application routine

WBOY
WBOYOriginal
2016-07-29 09:04:361061browse

Basic introduction to Session:

In PHP, each Session has an ID. This Session ID is an encrypted number randomly generated by PHP. This Session ID is stored in the client browser through a cookie, or passed directly to the client through the URL.

Session ID is like a key, used to register into the Session variable. These Session variables are stored on the server side. Session ID is the only painting data that exists on the client side.

Use Session ID to open the corresponding Session variable on the server side, and the session data related to the user will be clear at a glance. By default, Session variables on the server side are saved in the form of files, but session variable data is also often stored in the database.

Using Session you don’t need to manually set cookies, PHP Session can handle it automatically.

Simple comprehensive application:

1. Create a Session folder in the root directory of the website.

2. Create open.php in the Session folder, enter the following code and save it.

<?php 
session_start();
$_SESSION[&#39;name&#39;] = "admin";
echo "name:".$_SESSION[&#39;name&#39;];
?>
<a href=&#39;use.php&#39;>next</a>

3. Create use.php in the Session folder, enter the following code and save it.
<?php 
session_start();
echo "name:".$_SESSION[&#39;name&#39;];
?>
<a href=&#39;close.php&#39;>next</a>

4. Create close.php in the Session folder, enter the following code and save it.
<?php 
session_start() ;
unset($_SESSION[&#39;name&#39;]);
if (isset($_SESSION[&#39;name&#39;])){
	echo "name:".$_SESSION[&#39;name&#39;];
}else{
	echo "closed";
}
session_destroy();
?>

The above introduces the simple application routine of PHP Session, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn