PHP Session Management

Source: Internet
Author: User
Tags php session setcookie
read/Set expiry time for current cache
IntSession_cache_expire([string $new _cache_expire])
When the request starts, the cache expiration time is reset to 180 minutes and is saved in the Session.cache_expire configuration item. Therefore, for each request, you need toSession_Start() function call before calling Session_cache_expire () to set the cache expiration time. If given new_cache_expire, the current cache expiry time, in minutes, is set using the value of New_cache_expire and the default value is 180 (minutes).
read/Set cache limiter
StringSession_cache_limiter([string $cache _limiter])
The cache limiter defines the cache control policy in the HTTP response headers that are sent to the client. The client or proxy server detects the response header information to determine the caching rules for the page content. Set the cache limiter toNoCachePrevents the client or proxy server from caching content. PublicIndicates that the client or proxy server is allowed to cache content.PrivateIndicates that the client cache is allowed, 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 thePrivate_no_expireMode solves this problem: In this mode, the Expire response header is not sent to the client.
gets/sets the current session ID
Stringsession_id([string $id])
If the value of the ID parameter is specified, the specified value is used as the session ID. Must be called in theSession_Start() function before calling the session_id () function. Different session managers have different restrictions on the characters that can be used in the session ID. For example, the file session Manager only allows the session ID to use the following characters: A-Z A-Z 0-9, (comma) and-(minus sign).
read/Set session name
StringSession_name([string $name])
The Session_name () function returns the current session name. If you specify the name parameter, 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 to the Session.name configuration item. Therefore, to set the session name, for each request, you need to call theSession_Start() orSession_register() function before calling the Session_name () function. The session name requires at least one letter and cannot all use numbers, otherwise a new session ID is generated each time.
read/Set save path for current session
StringSession_save_path([string $path])
Specifies the path to which session data is saved. Must be called in theSession_Start() function before calling the Session_save_path () function. On some operating systems, it is recommended to save session data by using a path on a file system that can efficiently handle a large number of small size files. For example, on a Linux platform, the ReiserFS file system can provide better performance than the Ext2fs file system for the work of session data preservation.
start a new session or reuse an existing conversation
boolSession_Start(void)
Session_Start () creates a new session or reuses existing sessions. If you submit a session ID by either a GET or POST, or by using a cookie, the existing session is reused.
Destroy all data in a session
boolSession_destroy(void)

Session_destroy () destroys all data in the current session, but does not reset the global variables associated with the current session and does not 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 exits the login, the session ID must be reset at the same time. If the session ID is transferred through a cookie, the Setcookie () function is also called to remove the client's session cookie.

1, the session will determine whether there is currently $_cookie[session_name ()]; Session_name () returns the value of the COOKIE key that holds the session_id, which can be found from php.ini. If it does not exist, it generates a session_id and then passes the generated session_id as the value of the cookie to the client. The equivalent of performing the following cookie operation, note that this step performed the Setcookie() operation, A cookie is sent in the header of a header, which cannot be output before. PHP has another function, session_regenerate_id(), if you use this function, you cannot have the output before.
Setcookie (Session_name (), session_id (), session.cookie_lifetime,//default 0
session.cookie_path,//default '/' current program and directory are valid
session.cookie_domain,//default is empty
)
If there is so session_id =$_cookie[session_name ()], then go to session.save_path the specified folder to find the name ' Sess_ '. session_id () Files. The contents of the read file are deserialized and then placed in the $_session.


2, such as adding 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 session_id specified folder, and the related resources are closed.

3, the session sent to the cookie is generally an instant cookie, stored in memory, when the browser is closed, will expire, if you need to force expiration, such as log out, rather than close the browser, then you need to destroy the session in the code, there are many ways:
1]. Setcookie(Session_name (), session_id (), Time () -8000000,..); /Log out before execution
2]. usset($_session);//This will delete all $_session data, after the refresh, there is a cookie passed, but no data.
3]. Session_destroy();//This function is more thorough, delete $_session Delete session file, and session_id.
When the browser is not closed, refresh again, 2 and 3 will have a cookie sent over, but no data found.

 
  ";        Echo $_cookie[session_name ()];            Reset all variables in the session--use unset to reset a session variable//    $_session = Array ();    //Delete session 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"]);    }//    //Finally, destroy session//    Session_destroy ();?>
 
  Untitled Document

Copyright NOTICE: This article is the original article of Bo-Lang's work room, without the permission of Bo Master not to reprint.

The above introduces the PHP session management, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.