How to strictly control the session expiration time in PHP

Source: Internet
Author: User
Tags session id

The first kind of answer

Then, the most common answer is: Set the session expiration time, that is session.gc_maxlifetime, this answer is not correct, for the following reasons:

1. First, this PHP is using a certain probability to run the GC of the session, that is, session.gc_probability and session.gc_divisor (Introduction see PHP using the session encountered a permission Denied notice solution), this default value is 1 and 100, which is 1% chance, PHP will run the session GC when a session is started. There is no guarantee that it will expire in 30 minutes.

2. What about a chance to clean up a large probability? Or is it inappropriate, why? Because PHP uses the stat session file modification time to determine whether or not to expire, if the increase of this probability will degrade performance, and secondly, PHP uses "a" file to save and a session-related sessions variable, suppose I set up 5 minutes ago a a=1 session variable, 5 minutes later set up a b=2 seesion variable, then the session file modification time is to add the time of the B moment, then a can not be in 30 minutes, was cleared. There is also a third reason.

3. PHP Default (Linux for example), is the default storage directory using/TMP as the session, and the manual also has the following description:

Note: If different scripts have different session.gc_maxlifetime values but share the same place to store session data, the script with the smallest values cleans up the data. In this case, use this directive with Session.save_path.

That is, if two applications do not specify their own independent Save_path, one set the expiration time is 2 minutes (assuming a), one set to 30 minutes (assuming B), then each time A's session GC runs, it will also delete the session that belongs to application B Files.

So, the first answer is not "completely strict" right.

The second kind of answer

There is also a common answer is: Set the session ID of the carrier, the expiration time of the cookie, that is, session.cookie_lifetime. This answer is also incorrect for the following reasons:

This expiration is only a cookie expires, in other words, it examines the difference between a cookie and a session, the session expires when the server expires, and the cookie expires in the client (browser) to ensure that even if you set a cookie expires, This only guarantees that the standard browser expires, does not send this cookie (contains the session ID), and if you construct the request, you can still use the value of the session ID.

The third kind of answer

Using Memcache, Redis, etc., Okey, this answer is a correct answer. However, it is clear that the question will continue to ask you, if you just use PHP?

Fourth kind of answer

Of course, the interview is not for you, but to examine the thoroughness of thinking. In this process I will suggest these traps, so in general, the practice of conforming to test instructions is:

1. Set the cookie expiration time to 30 minutes and set the session's lifetime to 30 minutes.

2. Add a time stamp for each session value.

3. Before each visit, determine the timestamp.

Finally, some people ask, why set a 30-minute expiration date: This, first, this is for the interview, second, the actual use of the scene, such as the 30-minute expired coupons and so on

How to strictly control the session expiration time in PHP

Related Article

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.