session introduction
PHP Session
Start PHP Session
Before you store user information in the PHP session, you must first Start a session. Note: The session_start() function must be located before the <html> tag: <?php session_start(); ?><html><body></body></html>The above code will register the user's session with the server so that you can Start saving user information and assign a UID to the user session.Storing Session Variables
The correct way to store and retrieve session variables is to use PHP $_SESSION Variable:<?php session_start(); // 存储 session 数据 $_SESSION['views']=1; ?> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php // 检索 session 数据 echo "浏览量:". $_SESSION['views']; ?> </body> </html>Output: Views: 1In 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:
<?php session_start(); if(isset($_SESSION['views'])) { $_SESSION['views']=$_SESSION['views']+1; } else { $_SESSION['views']=1; } echo "浏览量:". $_SESSION['views']; ?>
Destroy Session
If you want to delete some session data, you can use the unset() or session_destroy() function. The unset() function is used to release the specified session variable:<?php session_start(); if(isset($_SESSION['views'])) { unset($_SESSION['views']); } ?>You can also completely destroy the session by calling the session_destroy() function:
<?php session_destroy(); ?>Note: session_destroy() The session will be reset and you will lose all stored session data.
Session has about 12 functions They are:
session_start: initial session. session_destroy: End session.
session_unset: Release session memory.
session_name: Access the current session name.
session_module_name: access the current session module.
session_save_path: access the current session path.
session_id: access the current session code.
session_register: Register new variables.
session_unregister: Delete registered variables.
session_is_registered: Check whether the variable is registered.
session_decode: Session data decoding.
session_encode: Session data encoding.
Before you store user information in a PHP session, you must first start the session.
Note: The session_start() function must be placed before the tag:
<html>
<body>
</body>
</html>
(1) Start session
Before each use of session, add this sentence: "session_start();". As the name suggests, the function of this function is to start using the session.
(2) Register session
First, create a global (note, it must be defined as global, otherwise it cannot be used on other pages) array, such as $login, where $login['name']="Victor" , $login['pwd']="111111", and then call the function "session_register(login);", the session is successfully registered.
(3) Using variables in the session
Similar to registering a session, you must first create a global array, and then it is the same as using a normal array.
(4) Determine whether the session is registered
It is very simple, just use "if (session_is_registered(login))" to judge.
(5) Uninstalling session
is also very simple, just "session_unregister(login);".
Note: Be sure to do (1) before doing (2) (3) (4) (5).