The first method is to set the php. ini configuration file and set the attribute values of the session. gc_maxlifetime and session. cookie_lifetime nodes. Of course, you can also use the ini_set function to change the attribute values of the current context:
The code is as follows: |
Copy code |
Ini_set ('session. Gc_maxlifetime', "3600"); // second Ini_set ("session. cookie_lifetime", "3600"); // second
|
The second method is to set the Session timestamp, for example, the following method.
When the logon succeeds, set the timestamp to 1 hour after the current time, $ _ SESSION ['expiretime'] = time () + 3600 ;. Use the following code to check user logon:
The code is as follows: |
Copy code |
If (isset ($ _ SESSION ['expiretime']) { If ($ _ SESSION ['expiretime'] <time ()){ Unset ($ _ SESSION ['expiretime']); Header ('Location: logout. php? Timeout'); // logout Exit (0 ); } Else { $ _ SESSION ['expiretime'] = time () + 3600; // refresh the timestamp } } |
Experience: in fact, the default session timeout time for php will take a while. Of course, we can set it according to the above method. I think it is more convenient to use cookies.