Don't quite understand PHP's session concept

Source: Internet
Author: User
Tags php session
The main is not quite understand how the life cycle of the session is calculated, it is how to carry the status information, hope everyone advice.

Reply content:

The main is not quite understand how the life cycle of the session is calculated, it is how to carry the status information, hope everyone advice.

In fact, the session is not a unique concept of PHP, all Web applications have to involve sessions. It is actually a mechanism for preserving state information.

Many people don't think about what state information is, or why there is a mechanism in the web. In fact, this is closely related to the characteristics of the Web, the web is not a long connection, after each request in the past, and the server disconnected, so that each request to the server is a new request, so it has no continuity, no continuity, no state.

But for us the state of being stateless is obviously unacceptable, and I can't ask the user to enter a username and password on every page. What is state, is the state that can be continued, regardless of the number of requests sent, I can maintain the proper state. So we're going to solve two problems.

    1. How to save a state
    2. How to get the status

So the session mechanism came into being, its browser and server interaction mechanism can be understood as

    1. The browser said to the server: "Hi, my dear, I came (to make the request), but every day there are countless people like me to come to you, I do not want to share with others that only belong to our memory (state)"
    2. The server said to the browser: "It doesn't matter, I have locked your memory in this box (session storage, file,memcached or something else), the key (session key) to You (Set-cookie)"
    3. The browser says, "OK, I put it in my purse." I'm gone, 88 (request complete) "
    4. The next day (second request), the browser again to find the server: "Hey, I came again, with the key in my purse, open your memory box."
    5. The server says: "Open up!" Really is you, want to die me ... "

There are two factors that affect the life cycle of the PHP session, one is whether the session information on the server side is still alive, and the other is whether PHPSESSID is still in the cookie on the browser side.
Server side session information, when the session is started to create, usually placed in a file, PHP will be activated when the GC is recycled
The PHPSESSID recorded in the client's cookie records a hash value that points to a server-side session, and when the cookie fails, the hash value is lost and the session expires.

Pass:

Phpinfo ();

You can see a series of configurations for the session, which can also affect the life cycle of the session.

PHP passes the session ID between requests, and then stores the session data in a server-side file or database, as well as in memory for performance improvements. There are three different ways:

1. Pass the session ID via the cookie header in the HTTP request
2. Pass the session ID via an HTTP GET request, that is, place the session ID in the URL parameter
3. Pass the session ID via an HTTP POST request, which is to place the session ID in the post data for delivery

I wrote an article on the nature of PHP session in my blog with http-http://www.360weboy.com/php/session-c .... If you are interested, you can read it carefully, if you find anything wrong, you can put it in the comments, I will check to correct. Want to share your technical ideas with more like-minded friends.

70 answer the good humor, good image. (and then accidentally wrote the wrong place.) )

  • 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.