PHP session will never expire solutions and implementation methods sharing _ PHP Tutorial

Source: Internet
Author: User
Tags php session
Solutions and implementation methods for PHP sessions that never expire. 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 this issue a few days ago. we developed a system that only the company's customer service staff can use in the early stage-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:

The code is as follows:


Session_start (); // start the Session
$ _ SESSION ['count']; // registers the Session variable count.
Isset ($ PHPSESSID )? Session_id ($ PHPSESSID): $ PHPSESSID = session_id ();
// If $ PHPSESSID is set, the SessionID is assigned to $ PHPSESSID. Otherwise, the SessionID is generated.
$ _ SESSION ['count'] ++; // variable count Plus 1
Setcookie ('phpsessid ', $ PHPSESSID, time () + 3156000); // store the SessionID to the Cookie
Echo $ count; // display the value of the Session variable count
?>


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.

Bytes. Just a few of our limited customer service staff suddenly raised this issue a few days ago...

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.