PHP session never expires. Solution and implementation method

Source: Internet
Author: User
Tags php session

So that the PHP session will never expire, you may not have encountered such a depressing problem, but I have encountered, very depressed.

We have developed a system that can only be used by our Customer Service staff-limited customer service staff. Just a few of our limited customer service staff suddenly raised the following question a few days ago: When we are worried about solving the customer's problem every very short time (not operating the page for half an hour, the system prompts you to log on, delaying the customer's time ...... This is unpleasant!

The customer is God, the only God. As a result, the peak demand is that we can achieve the PHP session never expire, unless our Customer Service Staff manually let him expire. For security reasons, I do not understand this kind of behavior that never expires. For laziness, I really don't want to modify the previous program. But there is no way. I still need to change it.

It is the best way not to modify the program, because if you modify the program, the test department will be very depressed, like me, so you can only modify the system environment configuration, in fact, it is very simple, open php. ini settings file. Modify the following three lines:

1. session. use_cookies

Set this value to 1 and use cookie to pass sessionid

2. session. cookie_lifetime

This indicates the time when SessionID is stored in the client Cookie. The default value is 0, indicating that the session ID will be voided once the browser closes it ...... This is why PHP sessions cannot be used permanently! So let's set it to a number that we think is big. How about 999999999? Yes! That's it.

3. session. gc_maxlifetime

This is the time when Session data is stored on the server. If this time is exceeded, Session data will be automatically deleted! Then we set it to 99999999.

So everything is okay. If you don't believe it, test it. Set a session value for 10 days and half a month, if your computer is not powered off or down, you can still see this sessionid.

Of course, you may not be able to modify php just as lucky as you do not have permission to control the server. ini settings, everything depends on ourselves. Of course, we must use the client to store cookies. The sessionID is stored in the client's cookie and the cookie value is set, then pass the value to the session_id () function. The procedure is as follows:

 

 
 
  1. <? Php
  2. Session_start (); // start the Session
  3. $ _ SESSION [count]; // registers the Session variable Count
  4. Isset ($ PHPSESSID )? Session_id ($ PHPSESSID): $ PHPSESSID = session_id ();
  5. // If $ PHPSESSID is set, the SessionID is assigned to $ PHPSESSID. Otherwise, the SessionID is generated.
  6. $ _ SESSION [count] ++; // variable count plus 1
  7. Setcookie (PHPSESSID, $ PHPSESSID, time () + 3156000); // store the SessionID to the Cookie
  8. Echo $ count; // display the value of the Session variable count
  9. ?>

If it takes a long time (how long? You can see it for yourself.) When you come back to refresh this page, the output number is 1 larger than when you go. That's right! If it's a lot bigger, it's estimated that someone has moved your computer. This test is inaccurate ...... Go out for a while!

Note: The 'phpsessid' in the setcookie line is not certain. If you have a network administrator with a modified mad disease, he may have modified it, the best way is to use the phpinfo () function to check the session. the value of name is relatively scientific.

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.