Easy to understand, Session work

Source: Internet
Author: User
Next, let's take a look at how the Session works. I wonder if you know how to implement identity authentication through cookies. First, generate a unique Cookie as a sign of user identity and register it in the database. Then, the Cookie passed by the user is compared with the Cookie registered in the database to determine the user's identity. The working principle of the Session is as follows. let's take a look at how the Session works. I wonder if you know how to implement identity authentication through cookies. First, generate a unique Cookie as a sign of user identity and register it in the database. Then, the Cookie passed by the user is compared with the Cookie registered in the database to determine the user's identity.
The same is true for sessions.
First, PHP generates a unique string for the user who creates the Session to mark the user's session. Generally, this string is called the Session Id. Then, "sess" + Session Id is the file name (for example, if a Session ID is 111, the file name is sess_111). create a file in the file system of the server, save the name and value of the global variable defined in the Session in the file. Then, save the Session Id as a Cookie named PHPSession in the file system of the user end.
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 user's PEESession Cookie, and saves the Session information from the file system of the server based on the Session Id. Finally, read the value of the global variable set during the last connection from this file.
Therefore, we can see that the working principle of Session is the same as that of identity authentication described in the previous section. The difference is that Session stores information in the file system of the server, and we store the information in the database. Of course, the advantage of using Session is that data storage and acquisition are automatically completed by PHP, and directly using cookies requires you to save and obtain data by yourself.
Session uses the Cookie identity flag function to save the information that users need to save when browsing the website on the server. In this way, Session not only overcomes the defects of the HTTP protocol, but also prevents information leakage and facilitates the use of programmers. it is a very good solution. However, the Session function is only supported by PHP4, and PHP3 does not support Session. Therefore, users who use PHP3 to build websites 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.