PHP Session Management _session session

Source: Internet
Author: User
Tags php session

First, understand the session

    • The session begins with the user access page, and the session's lifecycle is formed by disconnecting from the site. Each time a user connects, PHP automatically generates a unique sessionid to identify the current user and differentiate it from other users.

    • During the session, the PHP default generates an identity named Phpsessionid (which can be changed in the php.ini file), which accompanies each page being sent to the browser and then returns to the Web server with the next page request.

    • SessionID can be saved as session information in the database as a primary key (Primary key) to differentiate between different users, or as a unique string in the server-side session file name.

    • During session sessions, the SessionID is stored on both the client and server side two locations. On the client side, the temporary cookie is saved in the browser's designated directory (as a session cookie), and in the server, it is saved as a text file in the specified session directory.

Second, start the session

    • Creating a session from a session_start() function

      BOOL Session_Start (void);
      Description: session_start() The browser cannot have any output before using, otherwise an error will occur.

    • Creating a session from a session_register() function

session_register()The function is used to login a variable for the session implicitly to start the session, but requires the option to php.ini the file, set the register_globals instruction to ' on ', and restart the Apache server.

    • After the session variables are started, they are all saved in the global array $_SESSION[] . $_SESSIONcreating a Session variable from a global array is easy, just add an element directly to the array.

Third, the application session

    • The session in PHP is powerful: You can save specific data and related information for the current user. You can save an array, an object, a string arbitrary data type. To add various types of data to the session, you must apply a global array $_SESSION[] .

Iv. Delete Session

    • Delete a single session
      Delete the session variable, as with the array, and simply unregister $_SESSION an element of the array.

      unset ($_session[' what ');

    • Delete multiple sessions
      To unregister all session variables at once, you can assign an empty array to the$_SESSION

      $_session = Array ();

    • End Current session
      If the entire session has ended, you should first unregister all session variables and then use the session_destroy() function to clear the current session and empty all the resources in the session to completely destroy it.

      Session_destroy ();

V. Application of the session

    • Control the page access permissions through the session.

Vi. comparison of the cookie with the session

The biggest differences are:

    • Session is to save information on the server, and through a session ID to pass the client information, the server after receiving the session ID according to this ID to provide related session information resources;
    • A cookie is a situation in which all information is stored in a text file on the client and managed and maintained by the browser.
    • Session in the server storage, the client user can not modify the session file content. Cookies are stored on the client side, and the session is much safer than cookies.

The above describes the PHP session management _session session, 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.