Set the session expiration time in php

Source: Internet
Author: User
In apache and php environments, the default expiration time is about 20 minutes. how can we set the session expiration time? The following is a summary of the specific methods. The most common method is to set the following code in the php program to copy...

In apache and php environments, the default expiration time is about 20 minutes. how can we set the session expiration time? The following is a summary of the specific methods.

The most common method is to set

The code is as follows:

If (! Isset ($ _ SESSION ['last _ access']) | (time ()-$ _ SESSION ['last _ access'])> 60)
$ _ SESSION ['last _ access'] = time ();
?>

This is done. if you want to set an expiration date, you can also implement the instance in the program.

The code is as follows:

Unset ($ _ SESSION ['last _ access']) or $ _ SESSION ['last _ access'] = '';
?>

The session has an expiration mechanism.


Session. gc_maxlifetime the original session expiration is a small probability event, respectively using session. gc_probability and session. gc_pisor is used to determine the probability of gc sessions in the running session. gc_probability and session. the default values of gc_pisor are 1 and 100, respectively.

The probability of gc running in the session is 1% for the numerator and denominator respectively. If you modify these two values, the efficiency of php is reduced. So this method is not correct !!

Therefore, modifying the gc_maxlifetime variable in the php. ini file can prolong the session expiration Time: (for example, we can change the Expiration Time to 86400 seconds)

The code is as follows:

Session. gc_maxlife time = 86400

Then, restart your web service (generally apache.

When will session "recycle" occur?

By default, every php request will have a 1/100 probability of recovery, so it may be simply understood as "every 100 php requests will be recycled once ". This probability is controlled by the following parameters:

The code is as follows:

# The probability is gc_probability/gc_pisor.
Session. gc_probability = 1
Session. gc_pisor = 100


Note 1: assume that gc_maxlifetime = 120. if the last modification time of a session file is before 120 seconds, before the next recovery (1/100 probability, this session is still valid.

Note 2: If your session is saved elsewhere in session. save_path, the session recycle mechanism may not automatically process expired session files. In this case, you need to manually (or crontab) delete expired sessions: cd/path/to/sessions; find-cmin + 24 | xargs rm



Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.