Easy to understand, the way the session works (turn)

Source: Internet
Author: User
Tags file system session id php script variable
Let's take a look at how the session works under session. I wonder if you know how to authenticate by cookie. First, a unique cookie is generated as the identity of the user and registered in the database. It then controls the user's identity by comparing the cookie passed by the user to the cookie registered in the database.
This is how the session works.
First, PHP creates a unique string for the user who creates the session, which is used to mark the session of the user. This string is generally referred to as the session Id. Then the "Sess" +session ID is the filename (for example, a session ID is 111, then the file name is sess_111) creates a file in the server's filesystem that holds the variable name and value of the global variable defined by the user in the session. The session ID is then saved in the client's file system as a cookie named Phpsession.
Then, when the user connects to the server again to access a PHP script, PHP obtains the session Id of the user's session from the Peesession cookie that the user sends, and according to the session A file in which the ID holds session information from the server's file system. Finally, the value of the global variable set by the user at the last connection is read from this file.
So we can see that the session works the same way as the identity authentication we described in the previous section. The difference is only session to save the information in the server's file system, and we keep the information in the database. Of course, using the session benefits is the data to save and access is automatically completed by PHP, and the direct use of cookies will need to do their own data preservation and acquisition.
The session takes advantage of the cookie's identity flag function to store the information that the user needs to save on the server when browsing the site. This session not only overcomes the HTTP protocol flaw, but also prevents the information leakage, and facilitates the programmer's use, is a very good solution. However, the function of the session only PHP4 support, PHP3 is not support session. As a result, readers using PHP3 to build a Web site can only use cookies directly.


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.