How to set session expiration time in PHP _php tutorial

Source: Internet
Author: User
In the Apache and PHP environment, the default expiration time is about 20 minutes, then how we set the session expires, see the concrete method summarized below.

The most common thing we use in PHP programs is to set

The code is as follows Copy Code

if (!isset ($_session[' last_access ')) | | (Time ()-$_session[' last_access ')) >60)
$_session[' last_access '] = time ();
?>

This will be done, if you want to set the expiration, you can also in the program instance

The code is as follows Copy Code

unset ($_session[' last_access ') or $_session[' last_access ']= ';
?>

The session has an outdated mechanism


Session.gc_maxlifetime Original session expiration is a small probability event, using session.gc_probability and Session.gc_divisor respectively to determine the probability of GC in the running session The default values for Session.gc_probability and session.gc_divisor are 1 and 100, respectively.

The probability of a GC in the session is 1%, respectively, by numerator and denominator. If you modify these two values, you will reduce the efficiency of PHP. So this method is wrong!!

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

The code is as follows Copy Code

Session.gc_maxlifetime = 86400

Then, restart your Web service (typically Apache).

When does the session "recycle" occur?

By default, every PHP request will have a 1/100 probability of recycling, so it might simply be understood as "one recovery per 100 PHP requests". This probability is controlled by the following parameters

The code is as follows Copy Code

#概率是gc_probability/gc_divisor
session.gc_probability = 1
Session.gc_divisor = 100


Note 1: Assuming this situation is gc_maxlifetime=120, if the last modification time of a session file is 120 seconds ago, the session will still be valid until the next recovery (1/100 probability) occurs.

NOTE 2: If your session uses Session.save_path to save the session,session recycle mechanism, it is possible that the expired session file will not be processed automatically. At this time need to regularly manually (or crontab) Delete expired session:cd/path/to/sessions; Find-cmin +24 | Xargs RM

http://www.bkjia.com/PHPjc/631569.html www.bkjia.com true http://www.bkjia.com/PHPjc/631569.html techarticle in the Apache and PHP environment, the default expiration time is about 20 minutes, then how we set the session expires, see the concrete method summarized below. Our most commonly used is in the PHP program set ...

  • 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.