Another discussion of PHP session

Source: Internet
Author: User
Tags session id variables php session sessions unique id access

Reprint please indicate from Souldak, micro bo: @evagle

Previously on session and cookies are actually vague, today with the YII framework encountered SESSION_REGENERATE_ID (): Session object destruction failed this error, So I looked at the working mechanism of the session.


To intercept the definition of a session from W3cschool:

When you run an application, you open it, make some changes, and then close it. It's like a conversation. The computer knows who you are. It knows when you start the application and when it terminates. But on the internet, there is a problem: the server does not know who you are and what you do, because the HTTP address is not maintained.

The PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchase item, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.

The session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

Let's take a look at the session lifecycle, which is important for understanding the session.

1. When will the session be created?

Simply put, Sessinon is created when a user accesses the first access server, the session is created by a Web language such as php,jsp,asp, and access to static HTML pages is not created. Let's take PHP for example:

A when user x accesses a PHP page, if the page starts with the Session_Start () function, then the server will create a session, which will produce a session_id. This ID is unique and is different for each user.

b This session ID is stored in the PHP $_session variable, and we can set the cookie in the user's browser.

C then the user jumps to other pages of the site, as long as the browser to tell the cookie stored in the session ID, and then the server to take out the session ID corresponding to the stored information, you know who this user is, that on another page can also display the user's information.

D after the session is created, you can store other user-related information, such as storing the item information for the user's shopping cart.

E The server updates the session's last access time and maintains it as long as the user continues to access it once the session is generated. Every time the user accesses the server, whether or not the session is read or written, the server considers the user's session "active" once.


Therefore, the user visits a website, normally from open to the end (unless the session too fast failure) is in the same session, no matter how he jumps in this site, can access this session content.

The session_id () function in PHP can see the current session_id information. Because a site can be accessed by many users at the same time, PHP generates a unique session_id for each user. This does not interfere with each other because each user is processed by an independent process, and the process where user a resides stores the session_id of a, until the session fails.

In short, usually when the user log in to the site set up a session, and then the user quit the time to close or clean up the session (close or clean with the business requirements).

2. When does the session expire?

A more and more users will have access to the server, so there will be more sessions. To prevent memory overflow, the server removes inactive sessions from memory for a long period of time. This is the time of the session timeout. If the server is not accessed beyond the timeout period, the session is automatically invalidated.

b) Call the Invalidate method of the session.


Reference: http://www.w3school.com.cn/php/php_sessions.asp

Then a few of the most commonly used PHP session-related functions:

Session_start-start New or resume existing session start
Session_id-get and/or set the current session ID get session ID
Session_status-returns's session status gets session status, with three states

    • php_session_disabled if sessions are Disabled.
    • Php_session_none If sessions are enabled, but NONE exists.
    • php_session_active If sessions are enabled, and one exists. session _regenerate_id-update the current session ID with a newly generated one to reassign
      Session_destroy-destroys A for now session ll data registered to a session completely deletes the entire session
      Session_unset-free all sessions variables delete all variables stored in the current session


      /p>

      More please refer to: http://www.php.net/manual/en/ref.session.php








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.