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 Code is as follows: Copy code

<? Php
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: Copy code

<? Php
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_divisor is used to determine the probability of gc sessions in the running session. gc_probability and session. the default gc_divisor values 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: Copy code

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: Copy code

# The probability is gc_probability/gc_divisor.
Session. gc_probability = 1
Session. gc_divisor = 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

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.