Under what circumstances will it be called to Session_destroy ()

Source: Internet
Author: User
When a page is accessed, a session Id,session_destroy () will be generated when it is called. Close the browser? Close this page? Or does the page go to another address? Session_destroy () is it automatically called?

Please explain in detail

Reply content:

When a page is accessed, a session Id,session_destroy () will be generated when it is called. Close the browser? Close this page? Or does the page go to another address? Session_destroy () is it automatically called?

Please explain in detail

First of all ... session_destory () is a function ...

This function is not automatically called by the PHP engine under any circumstances ... You can only manually call ...

There is a mechanism for clearing the session inside PHP ... But it's completely unrelated to this function ...

If you want to ask when it is time to call this function manually ... The answer is when you want to completely clean out the current session ...

There are several situations mentioned in the question ... Whether it is to close the page or ... Close your browser ... Even if you remove the browser from the reload ...

will not affect the session that has already been generated ... Word... All browser behavior will not cause the session to be automatically destroyed ...

So ... how does PHP actually clean up the session?

Read php.ini carefully ... You can find the following lines ...

; Defines the probability, the ' garbage collection ' process is started; On every session initialization. The probability is calculated by using; Gc_probability/gc_divisor. Where session.gc_probability is the numerator; And Gc_divisor are the denominator in the equation. Setting this value to 1; When the Session.gc_divisor value was give you approximately a 1%; Chance the GC would run on any give request.; Default value:1; Development value:1; Production value:1; http://php.net/session.gc-probabilitysession.gc_probability = 1; Defines the probability, the ' garbage collection ' process is started; On every session initialization. The probability is calculated by using; The following equation:gc_probability/gc_divisor. Where session.gc_probability; is the numerator and session.gc_divisor are the denominator in the equation. ; Setting this value to 1 then the Session.gc_divisor value is give you; Approximately a 1% chance the GC would run on any give request. INcreasing this; Value to give a 0.1% chance the GC would run on any give request. ; For the volume production servers, this is a more efficient approach.; Default value:100; Development value:1000; Production value:1000; Http://php.net/session.gc-divisorsession.gc_divisor = 1000; After this number of seconds, stored data would be seen as ' garbage ' and; Cleaned up by the garbage collection process.; Http://php.net/session.gc-maxlifetimesession.gc_maxlifetime = 1440

In fact, the English comments have been very clear ... But if you don't want to see ... I can explain it to you.

Due to the working mechanism of PHP ... It does not provide daemon to periodically scan session information and determine if it is invalid ...

When you call Session_Start () every time ...

PHP will session.gc_probability session.gc_divisor decide whether to enable garbage Collector based on and.

Garbage Collector as the name implies ... be called 垃圾回收器 ... Also known as the GC ...

Some specific ... In the configuration file I just posted ...

session.gc_probability = 1session.gc_divisor = 1000

That means PHP will have a 1 per thousand chance of starting garbage collection ...

The job of garbage collection is to scan all existing sessions under the session storage path session.save_path ...

Then the current time minus the last modification time of each session is session.gc_maxlifetime compared with the parameter ...

If a session's lifetime exceeds session.gc_maxlifetime the set value, destroy it ...

In fact, this process is completely PHP engine behavior ... Nothing to do with your program ... And what the user has done is irrelevant ...

User closes browser and opens ... Because the cookie fails he will get a new session ...

But this does not mean that his original session was destroyed ... The session still exists on the server ...

If he manually changed the value of the cookie that the name equals to the session.name previous session_id () ...

You can still get back to the previous session.

Another situation ... If a user gets a session after a long time without any action ...

He may lose his session because other users have triggered a garbage collection ...

This is basically the case ... Something more detailed ... You can refer to the section on garbage collection in the PHP manual ...

Well... That's it.

Typical situation: When the user logs off; session_id expires.

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