Home > Backend Development > PHP Tutorial > PHP Session Management

PHP Session Management

WBOY
Release: 2016-08-08 09:21:04
Original
991 people have browsed it
Read/set the expiration time of the current cache
int session_cache_expire ([ string $new_cache_expire ] )
When the request starts, the cache expiration time will be reset to 180 minutes and saved in the session.cache_expire configuration item. Therefore, for each request, session_cache_expire() needs to be called before the session_start() function call to set the cache expiration time. If new_cache_expire is given, use the value of new_cache_expire to set the current cache expiration time in minutes. The default value is 180 (minutes).
Read/Set cache limiter
                                                                                                                using     using                                       out out out out out of The client or proxy server determines the caching rules for the page content by detecting this response header information. Set the cache limiter to nocache will prohibit the client or proxy server from caching content,
public means that the client or proxy server is allowed to cache content, private means that the client is allowed to cache, but the proxy server is not allowed to cache the content. In private mode, some browsers, including Mozilla, may not handle the Expire response header correctly by using private_no_expire mode can solve this problem: in this mode, the Expire response header will not be sent to the client. Get/Set the current session ID
​ string session_id ([ string $id ] )
​ If the value of the id parameter is specified, use the specified value as the session ID. The session_id() function must be called before calling the session_start() function. Different session managers have different restrictions on the characters that can be used in session IDs. For example, File Session Manager only allows the following characters in session IDs: a-z A-Z 0-9 , (comma) and - (minus sign).
Read/set session name
  string session_name ([ string $name ] )
  The session_name() function returns the current session name. If the name parameter is specified, the session_name() function updates the session name and returns the original session name. When the request starts, the session name is reset and stored in the session.name configuration item. Therefore, to set the session name, for each request, you need to call The session_name() function is called before the session_start() or
session_register() function. The session name needs to be at least one letter and cannot be all numbers, otherwise a new session ID will be generated each time. Read/set the save path of the current session
  string session_save_path ([ string $path ] )
  Specify the path to save session data. The session_save_path() function must be called before calling the session_start() function. On some operating systems, it is recommended to save session data using paths on a file system that can efficiently handle large numbers of small-sized files. For example, on Linux platforms, the reiserfs file system will provide better performance than the ext2fs file system for session data saving.
Start a new session or reuse an existing session
  bool session_start ( void )
  session_start() will create a new session or reuse an existing session. If a session ID is submitted via GET or POST, or using a cookie, the existing session will be reused. Destroy all data in a session

bool session_destroy ( void )

session_destroy() Destroy all data in the current session,
But it will not reset the global variables associated with the current session, nor will it reset the session cookie.

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
'SESS_'.session_id(). Read the content of the file and deserialize it. Then put it in $_SESSION.


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(&#39;Session_test&#39;);
    session_id(&#39;safdsadfasdfsadfasdf&#39;);
    session_start();
    
    $_SERVER[&#39;user&#39;] = &#39;admin&#39;;
    echo $_SERVER[&#39;user&#39;]."<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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template