The solution and implementation method of the session never expires in PHP _php tutorial

Source: Internet
Author: User
Tags php session
Let PHP session never expire, you may not have encountered such a depressing problem, but I met, very depressed.

We developed a system that only the company's customer service staff can use--a limited number of customer service personnel. It is this limited number of customer service personnel suddenly raised the question a few days ago: we every very short period of time (half an hour do not operate the page), is anxious to solve customer problems, the system is prompted to login, delay the customer's time ... This is very unpleasant!

The customer is God, the only God. So the peak asks us to be able to realize that the session in PHP never expires, unless our customer service personnel let him expire. For security reasons I do not understand this never-expiring behavior; I really don't want to change the previous program for the sake of laziness. But there is no way, I still need to change.

Do not modify the program is the best way, because if you modify the program, the test department must be very depressed like me, then can only modify the system environment configuration, is actually very simple, open php.ini settings file, modify three lines as follows:

1, Session.use_cookies

Set this value to 1 and use a cookie to pass the SessionID

2, Session.cookie_lifetime

This represents sessionid the time that the client cookie is stored, the default is 0, which means that the browser shuts down SessionID. This is why the session of PHP cannot be used permanently! So let's set it to a number we think is big, 999999999 how, yes!

3, Session.gc_maxlifetime

This is the session data on the server side of the storage time, if more than this time, then the session data will be automatically deleted! Then we also set it to 99999999.

That's all OK, of course, if you don't believe it, just test it--set a session value after a 10-day half-month return to see if your computer is not powered down or down, you can still see this sessionid.

Of course, you may not have control of the server permissions and not as fortunate as I can modify the php.ini settings, all depend on ourselves there is a way, of course, you must use the client to store cookies, the resulting sessionid stored in the client's cookie, Set the value of this cookie and pass this value to the function session_id (), as follows:

 
 
  1. session_start (); //Start session
  2. $_session [Count]; //Register Session variable count
  3. isset ($PHPSESSID)? session_id ($PHPSESSID): $PHPSESSID = session_id ();
  4. //If $PHPSESSID is set, assign SessionID to $phpsessid, otherwise generate SessionID
  5. $_session [count]++; //Variable count plus 1
  6. Setcookie (phpsessid, $PHPSESSID, Time () +3156000); //Store SessionID into cookies
  7. Echo $count; //Display the value of the session variable count
  8. ?>

If long after (how long?) you come back to refresh this page, the output of the number is 1 bigger than when you go that's right! If a lot bigger, it is estimated who moved your computer, this test is not accurate, hehe ... Go out for a while!

Note: The ' Phpsessid ' in the Setcookie line is not certain, and if you encounter a network administrator with a modified mania, he may have modified it, and the best way is to use the phpinfo () function to see Confirm the value of Session.name one item, compare science.

http://www.bkjia.com/PHPjc/486271.html www.bkjia.com true http://www.bkjia.com/PHPjc/486271.html techarticle let PHP session never expire, you may not have encountered such a depressing problem, but I met, very depressed. We developed a system that only the company's customer service personnel can use ...

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