Home >Backend Development >PHP Tutorial >PHP Session Management
If you need to use session variables again, you must call the session_start() function again. In order to completely destroy the session, such as when the user logs out, the session ID must also be reset. If the session ID is transmitted through a cookie, the setcookie() function also needs to be called to delete the client's session cookie. 1. The session will determine whether there is currently $_COOKIE[session_name()];session_name() returns the COOKIE key value that saves the session_id. This value can be found from php.ini. If it does not exist, a session_id will be generated, and then the generated session_id will be passed to the client as the value of COOKIE. It is equivalent to executing the following COOKIE
Operation, please note that this step executes the setcookie() operation, and the COOKIE is sent in the header. There cannot be output before this. PHP has another function
session_regenerate_id(), if you use this function, there will be no output before this.
setcookie(session_name(), session_id(), session.cookie_lifetime,//Default 0
set cookie
Session.cookie_domain,//Default is empty
)
If it exists, then session_id =$_COOKIE[session_name()]; Then go to the folder specified by session.save_path to find the file named
2. For example, add a new value $_SESSION['test'] ='blah'; then this $_SESSION will only be maintained in memory. When the script execution ends, The value of $_SESSION is written to the folder specified by session_id, and then the related resources are closed.
3. The COOKIE sent by SESSION is generally an instant COOKIE and is stored in memory. It will expire when the browser is closed. , if you need to force expiration manually, such as logging out instead of closing the browser, then you need to destroy the SESSION in the code. There are many methods:
1]. setcookie(session_name(),session_id(),time() - 8000000,..);//Execute before logging out
2]. usset($_SESSION);//This will delete all $_SESSION data. After refreshing, COOKIE is passed, but there is no data.
3]. session_destroy();//This function is more thorough, delete $_SESSION, delete the session file, and session_id.
When you refresh the browser again without closing the browser, COOKIE will be sent to 2 and 3, but the data cannot be found.
<?php // 初始化会话 session_name('Session_test'); session_id('safdsadfasdfsadfasdf'); session_start(); $_SERVER['user'] = 'admin'; echo $_SERVER['user']."<br>"; echo $_COOKIE[session_name()]; //// 重置会话中的所有变量 -- 使用unset可以重置某个会话变量 // $_SESSION = array(); // // 同时删除会话 cookie // if (ini_get("session.use_cookies")) // { // $params = session_get_cookie_params(); // setcookie(session_name(), '', time() - 42000, // $params["path"], $params["domain"], // $params["secure"], $params["httponly"]); // } // // 最后,销毁会话 // session_destroy(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> </body> </html>
Copyright statement: This article is an original article by the blogger Lang Ya Studio and may not be reproduced without the permission of the blogger.
The above has introduced PHP Session management, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.