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. We usually set in the php program

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.

We usually set it in a php program. The code is as follows:

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

This is done. if you want to set expiration, you can also implement it in the program. The code is as follows:

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

Mechanism of session validity expiration

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 values of gc_divisor are 1 and 100, respectively, the numerator and denominator. Therefore, the probability of running gc in the session is 1%. if you modify these two values, the efficiency of php is reduced, so this method is not correct!

Therefore, modify the gc_maxlifetime variable in the php. ini file to extend the expiration time of the session: (for example, modify 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:

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