PHP Session GC Mechanism under window and Ununtu is different.

Source: Internet
Author: User
Tags php session php write
See the previous mr_jing of the session article, benefited. It happened that I also met the question of the session under win. He mentioned the last interesting thing in the article, when I saw the feeling is, too TMD (sweet) fate. And then I myself the whole of a water diary I understand the Session_set_save_handler execution sequence mechanism, can still not be reconciled, actually look at the Linux under the session in the end can not, (now win under certainly can't).

Test the session GC mechanism under win

  • First explain the environment-win under the Phpstudy
  • 2.php.ini Configuration

    3, the code starts write.php write session, the value is the current timestamp


    4. Read the session Code

       "; Echo ' gc_probability '. Ini_get (' session.gc_probability ')."
    "; Echo ' Gc_divisor '. Ini_get (' Session.gc_divisor ')."
    "; Session_Start (); Echo '. ' Session::=> '; var_dump ($_session); Echo '. Cookie::=> '; var_dump ($_cookie); echo "; echo"
    Readtime ". Date (' y-m-d h:i:s ', Time ());

    5, the implementation of read.php
    See Write Time of 17:57:04

    6, read the session, you can see the value of reading session (above the red Oval circle) time and write time are the same, are
    17:57:04, note that the following readtime line is 13:58:56,

    Well, as of now, there's no problem with win. Because even if your session's maximum time to Live (Session.gc_maxlifetime) is 5 seconds, your GC ratio is 1:1, which is the percentage of floors.
    But note:
    GC Recycling starts with your session_start, then executes the open,read, then the GC.
    You can click here to see the order of execution of the session.

    So even if a session has expired, you read him, or you have a value, and the next time you get a GC off.
    (Some friends may think, that the first read has a value, that judge the user login is not accurate, ah, but the real production environment, certainly not a single user, triggering the session_start mechanism of a lot of situations, so it is not surprising. )

    So here's the problem!!
    When we keep on the win platform Refresh read.php This page, the value of the session has been able to get. This is strange, should be recycled AH. How can you keep getting it? Understanding the session GC is based on the last modification time of the file compared to the maxlifetime time set in the current time and INI, and then triggers the recycle mechanism.

    Let's take a picture.

    This is where the wonderful
    1, if the session is deleted, and then in direct write, found that the creation time and just deleted before the creation time. I can understand the same name, but the same time is swollen! Windows lazy took the Recycle Bin back? I emptied him, too.
    Attach a GIF (I am how boring ~, please use IE browser to open this image, or beauty 美图秀秀, ACDSee, etc.)

    GC for the session under Linux

    My locally built VirtualBox (lamp under Ubuntu)
    Configuration such as:

    Then in a dynamic graph: Show write session, and then view the status of the session file, then read, the first read, is a value, and two times to view the stat sess** file is still in, I refresh once, then the value is not. It also verifies that the session process is open read (GC) write close. Note that the first time I have a value that is open before, not read the value of write

    Written at the end:
    The article wrote a little coarse, disorderly up eight.
    Finally, thank you for your predecessors ' advice, especially cfc4nx.

    Write clearly next time.

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