How to set a session with a strict 30-minute expiration date
Today on my Weibo (laruence) issue a question:
In the interview, I often ask a question: "How to set up a 30-minute expired session?", we do not think it seems simple, here the bread contains a lot of knowledge, especially suitable for investigating the fundamentals is solid, who will answer to try? Oh
Why do you ask this question? 1. I saw someone talking about this on Twitter, 2 thought I often asked this question, so ~ ~
Here, let me answer the question.
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 a certain probability to run the session of the GC, that is, session.gc_probability and session.gc_divisor (Introduction to the deep understanding of PHP principles of the session GC of a small probability notice), this default value is 1 and 100, is also a 1% chance, PHP will be at the start of a session, run the session GC. It is not guaranteed to expire at 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, a classmate asked, why to set the expiration time of 30 minutes: this, first of all, this is for the interview, second, the actual use of the scene, such as 30 minutes on the expiry of the coupon?
Thanks
--------------------------------
Http://blog.sina.com.cn/s/blog_8155e74d0101iqmh.html
http://blog.csdn.net/xiaojun1288/article/details/7351618