A detailed description of the functions used in the session in PHP

Source: Internet
Author: User
Tags session id php session php website setcookie

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.

The session_start() session Manager's open and read callback functions are invoked internally by PHP when the session is started automatically or by manual start. The session Manager may be PHP default or extended (SQLite or Memcached extensions), or it may be a session_set_save_handler() custom session Manager by setting the user. With the existing session data returned by the read callback function (stored in a special serialization format), PHP automatically deserializes the data and populates the $_SESSION Super global variable.

To use a named session, call the Session_name () function before calling the Session_Start () function.
If the session.use_trans_sid option is enabled, the session_start() function registers an internal output manager that completes the work of URL rewriting.

Note: To use a cookie-based session, you must call the Session_Start () function before the output begins.

Session_unset ()

Destroys the current session data. or $_SESSION = array(); if you want to destroy individual session data, you can unset ($_SESSION['varname']);.

Session_destroy ()

session_unset()Unlike destroying all session data, the session_destroy() session itself is destroyed, and when this function is out, the current session becomes garbage and waits for the GC mechanism to reclaim it. However, it does not reset the global variables associated with the current session, nor does it reset the session cookie. The session data is still in, but the "identity" has changed. If the GC has not yet recycled it, then it is still readable, which is certainly not to be seen, so it is often a session_destroy() session_unset() bit earlier.

If you need to use session variables again, you must recall the session_start() function. 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.

The following example destroys the data itself:

Initializes the session. If you want to use a session, don't forget to call it now: session_start ();//Reset all variables in the session, destroy session data $_session = Array (); or Session_unset ()//If you want to clean up more thoroughly, then delete the session cookie//Note: This not only destroys the data in the session, but also destroys the session itself 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 ();

Destroys session data $_SESSION = array() or session_unset() destroys the session itself session_destroy() ; setcookie() session_destroy()after execution, the session becomes a garbage session, waiting for the GC mechanism to be recycled.

Session_commit ()

session_commit()It's a session_write_close() nickname. And session_start instead, it is written and closed . The current session data is saved and the current conversation is closed. To prevent a concurrent write session, only one PHP script can be allowed to operate the session at any one time, so once a script is session_start opened, session_write_close() no other script can use the session until the script is terminated or called. The session is automatically written and closed by default at the end of the script, but when the script executes for a long time, the script consistently occupies the lock so that no other script can use the session, resulting in many errors. Therefore, the best practice is that any session variables, data changes (such as $_SESSION[xx] = xxx ), should be used to save data in a timely manner session_commit() , close the conversation.

SESSION_ID ()

SESSION_ID () can be used to get/set the current session ID. In order to be able to attach the session ID to the URL conveniently, you can use the constant SID to get the session name and ID expressed in string format. Simply put, it is if you open the Post/get method to get the session ID, then this parameter can be session.use_trans_sid set and visible in the URL, and in the script through the global variable SID to get directly.

Configure Ini_set (' Session.use_trans_sid ', ' Sid '),//url performance 127.0.0.1?sid=xxxxx//directly get $sid = SID;

If the session_id () value of the parameter is specified, the specified value is used as the session ID. The function must be session_start() called before the function is called session_id() . Different session managers have different restrictions on the characters that can be used in the session ID. If no session is currently present, an empty string ("") is returned.

Here are two points worth noting:

    • If the session ID is transferred using a cookie, and the ID parameter is specified, a new cookie is sent to the client after calling Session_Start (), regardless of whether the current session ID and the newly specified session ID are the same

    • Before switching the current session ID through session_id ($id), you need session_commit() to close the previous session, or the current session is still the original

More functions can be found in the PHP website or manual.

In the previous article we talked about the principles and best practices of the session. Now, let's talk about some of the related functions that the PHP session uses.

Session_Start ()

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.

The session_start() session Manager's open and read callback functions are invoked internally by PHP when the session is started automatically or by manual start. The session Manager may be PHP default or extended (SQLite or Memcached extensions), or it may be a session_set_save_handler() custom session Manager by setting the user. With the existing session data returned by the read callback function (stored in a special serialization format), PHP automatically deserializes the data and populates the $_SESSION Super global variable.

To use a named session, call the Session_name () function before calling the Session_Start () function.
If the session.use_trans_sid option is enabled, the session_start() function registers an internal output manager that completes the work of URL rewriting.

Note: To use a cookie-based session, you must call the Session_Start () function before the output begins.

Session_unset ()

Destroys the current session data. or $_SESSION = array(); if you want to destroy individual session data, you can unset ($_SESSION['varname']);.

Session_destroy ()

session_unset()Unlike destroying all session data, the session_destroy() session itself is destroyed, and when this function is out, the current session becomes garbage and waits for the GC mechanism to reclaim it. However, it does not reset the global variables associated with the current session, nor does it reset the session cookie. The session data is still in, but the "identity" has changed. If the GC has not yet recycled it, then it is still readable, which is certainly not to be seen, so it is often a session_destroy() session_unset() bit earlier.

If you need to use session variables again, you must recall the session_start() function. 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.

The following example destroys the data itself:

Initializes the session. If you want to use a session, don't forget to call it now: session_start ();//Reset all variables in the session, destroy session data $_session = Array (); or Session_unset ()//If you want to clean up more thoroughly, then delete the session cookie//Note: This not only destroys the data in the session, but also destroys the session itself 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 ();

Destroys session data $_SESSION = array() or session_unset() destroys the session itself session_destroy() ; setcookie() session_destroy()after execution, the session becomes a garbage session, waiting for the GC mechanism to be recycled.

Session_commit ()

session_commit()It's a session_write_close() nickname. And session_start instead, it is written and closed . The current session data is saved and the current conversation is closed. To prevent a concurrent write session, only one PHP script can be allowed to operate the session at any one time, so once a script is session_start opened, session_write_close() no other script can use the session until the script is terminated or called. The session is automatically written and closed by default at the end of the script, but when the script executes for a long time, the script consistently occupies the lock so that no other script can use the session, resulting in many errors. Therefore, the best practice is that any session variables, data changes (such as $_SESSION[xx] = xxx ), should be used to save data in a timely manner session_commit() , close the conversation.

SESSION_ID ()

SESSION_ID () can be used to get/set the current session ID. In order to be able to attach the session ID to the URL conveniently, you can use the constant SID to get the session name and ID expressed in string format. Simply put, it is if you open the Post/get method to get the session ID, then this parameter can be session.use_trans_sid set and visible in the URL, and in the script through the global variable SID to get directly.

Configure Ini_set (' Session.use_trans_sid ', ' Sid '),//url performance 127.0.0.1?sid=xxxxx//directly get $sid = SID;

If the session_id () value of the parameter is specified, the specified value is used as the session ID. The function must be session_start() called before the function is called session_id() . Different session managers have different restrictions on the characters that can be used in the session ID. If no session is currently present, an empty string ("") is returned.

Here are two points worth noting:

    • If the session ID is transferred using a cookie, and the ID parameter is specified, a new cookie is sent to the client after calling Session_Start (), regardless of whether the current session ID and the newly specified session ID are the same

    • Before switching the current session ID through session_id ($id), you need session_commit() to close the previous session, or the current session is still the original

More functions can be found in the PHP website or manual.

Related Article

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.