The default session validity period in PHP is 1440 seconds (24 minutes). What should we do if we want to keep the session from expired? Next we will introduce the best way to do this without modifying the program, if you modify the program, the test department will be very depressed, just like me, so you can only modify the system environment configuration, in fact, very simple, open php. ini setting file,
Modify the three rows as follows:
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.
If you cannot modify the php. ini file
The Code is as follows: |
Copy code |
<? Php 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 ?> |