The session expires in PHP thinking one or two

Source: Internet
Author: User

read an article about PHP setup session expiration (http://www.laruence.com/2012/01/10/2469.html) by a member of the PHP development teamHe also said that the general people answer a few answers, answer other people's questions is also a process of their own thinking, and their own blog, using code word to promote their own thinking,so I wrote this reading NOTE--!  his question."When I'm interviewing, I often ask a question:" How do I set up a 30-minute session? "You don't think it seems simple, there's a lot of knowledge about bread here ,especially suitable for investigating whether the basic skills are solid, who will answer to try " One: FirstLet us recall the birth of the session, which is to say with the characteristics of HTTP, because HTTP is a stateless protocol, once accessed, the link is broken,in order for the client to establish a connection with the server, so that the cookie came into being, how he passed from the client to the server side, of course, through the HTTP protocolThe session is transmitted via a cookie to the server-sidethere 's a picture of the truth   Second: The session in the PHP.ini setting First scenario (wrong answer)there is an expiration time session.gc_maxlifetime, this can set the session expiration time, if we set 30 minutes expired, but look at the bird Brother's blog, said this is a small probability event, it is determined by 2 parameterssession.gc_probility and Session.gc_divisor (see: http://www.laruence.com/2011/03/29/1949.html), this default value is 1 and 100, respectively, That is, there is a 1% chance that PHP will start at a session,run the session GC. There is no guarantee that it will expire in 30 minutes. then brother Bird told the session on the class *unix server, how PHP determines the session expiration time:On the server, each session is stored independently in a file, PHP.ini is set Session.save_path, class *unix is generally placed in/TMP, and the calculation session expiration time is through Stat Session File Modification date, he said if modified to the big probability that the problem arises in the world: One : Performance issues Two : If the session file writes A = 10, expires after 5 minutes, and then 3 minutes later writes B =then the modification date of this file changed, then the session expires time also changed, then a can not be cleared in 5 minutes Three : There's a third reason, I copied it.

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.

The second type (wrong answer)

Set the expiration time of a cookie

Session.cookie_lifetime. This parameter to set the cookie expiration time, this is more unreliable, the cookie is the client, the session is the server

The Third Kind: (right)

Use the memcached Redis k-v database to store the session

  The Fourth kind: (The best Answer)copy his content, I'm a little lazy .

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?

finally put a piece of code, I have the code to test the wood
1 functionSessexpire ($expire= 0)2 {3     if($expire= = 0){4         $expire=Ini_get("Session.gc_maxlifetime");5}Else {6         Ini_set("Session.gc_maxlifetime",$expire);7     }8     9     if(Empty($_cookie[PHPSESSID]) {Ten         Session_set_cookie_params($expire); One         Session_Start(); A}Else { -         Session_Start(); -         Setcookie(PHPSESSID,session_id(), Time() +$expire); the     } -}

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.