The failure time and expiration recovery mechanism of PHP session control

Source: Internet
Author: User
Tags php session

PHP Session control expiration time and expiration recovery mechanism session life cycle
从session的初始化开始,直到注销的这段时间称之为sesssion生命周期。
Set the correlation parameters in the session life cycle re-php.ini
session.save_path    设置保存的session文件路径。session.use_cookies = 1    设置为1时,利用cookie来传递sessionidsession.cookie_lifetime = 0 (默认为0)    设置sessionid在客户端cookie存储的时间,默认为0,即关闭浏览器就失效。session.gc_maxlifetime = 1440(默认值 单位为秒)    设置session存活时间    原理:每次GC启动后,会通过stat得到session文件最后访问的unix时间,    通过现在的时间减去最后访问的时间,大于session.gc_maxlifetime,则认为    该session已过期,但该文件并没有被删除。因为php5的session采用被动回收    机制,过期的session是不会自己消失的,而是通过触发回收机制来处理过期的    session。下面两个参数为回收机制的配置。session.gc_probability = 1(默认值)session.gc_divisor = 1000 (默认值)    这两个配置决定了gc的概率,默认为1/1000。也就是不是每个session信息都有    100%的概率被系统当做垃圾来处理的。    意味着每1000次请求会启动一次gc回收session。    因为启动gc进程会影响php的执行效率,所以频率不易太频繁。注意:gc仅会处理session.save_path中的session文件。
Attention:
1.如果session文件没有及时回收,达到GB或更大级别的时候,就会影响该站点存取session的速度,进而影响相关功能。2.有一种情况,当某用户的会话是活跃的,只是session文件一直未修改,但是系统认为它是失效的,导致session文件被‘误删除’。怎么能避免这种情况呢?
<?php //手动去修改session文件的最后修改时间。if(!isset($_SESSION[‘last_access‘])||(time()-$_SESSION[‘last_access‘])>60){     $_SESSION[‘last_access‘] = time(); }?>
反之手动设置过期该怎么办呢?
<?php     unset($_SESSION[‘last_access‘]);    //也可以这样 $_SESSION[‘last_access‘]=‘‘; ?>
Set session in PHP never expires
通过上述参数设置描述,可否想到如何能让session用不过期?1.session.cookie_lifetime = 99999999,前提是session.use_cookies = 12.session.gc_maxlifetime = 99999999

The failure time and expiration recovery mechanism of PHP session control

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.