Set the Session timeout logon time limit in Php

Source: Internet
Author: User
Tags current time

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.

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.