PHP session过期的机制和配置/session回收利用
postedbyfengon 2010/05/11 leave a comment (0) Go to comments
使用PHP session的话,会遇到明明已经过了session的有效期,session却依然完好无损地活着的情况,头会变大。 实际上,仔细看看php.ini的PHP session回收利用机制,一目了然。
session回收机制:
PHP使用垃圾合并流程回收过期的会话,但并不是每次建立会话时都能唤起“垃圾合并流程”,gc以一定的概率启动。 这主要是考虑到服务器的性能方面,每次session都会触发gc,如果浏览数多的话服务器就不能吃了,但是如果以一定的概率打开gc,在流媒体数多的情况下,session的有效期限机制就会正常运行,服务器就会有效细节应该都是多年的经验积累起来的。
与PHP session过期相关的三个参数:
session.gc_probability=1
session.gc_divisor=1000
session.gc_maxlifetime=1440
gc启动概率=GC _ probability/GC _ divisor=0.1 %
session有效期限gc_maxlifetime单位:秒
建议:
自己测试的时候,当然这个session的有效期限的概率设定得越大,有效期限的效果就越明显。
web服务正式提供后,session的过期概率必须根据web服务的浏览量和服务器的性能综合考虑session的过期概率。 为每个session打开gc,显然是不明智的。
死板的方法:
在代码中手动设置session的上次访问时间,判断每次访问的访问间隔是否超过时间限制,如果没有超过手动放弃session,则更新上次访问时间。 此方法还可以限制两次访问间隔。
个人资料摘录(来自php.ini ),英文原件
; definestheprobabilitythatthe‘garbage collection’processisstarted
; oneverysessioninitialization.theprobabilityiscalculatedbyusing
; GC _ probability/GC _ divisor.where session.GC _ probabilityisthenumerator
; and GC _ divisoristhedenominatorintheequation.settingthisvalueto 1
; when the session.GC _ divisorvalueis 100 willgiveyouapproximatelya1% chance
; the gc will run on any give request。
; Default Value: 1
; 开发价值: 1
; Production Value: 1
; 3358 PHP.net/session.GC-probability
session.gc_probability=1
; definestheprobabilitythatthe‘garbage collection’processisstartedonevery
; sessioninitialization.theprobabilityiscalculatedbyusingthefollowingequation 3360
; GC _ probability/GC _ divisor.where session.GC _ probabilityisthenumeratorand
; session.GC _ divisoristhedenominatorintheequation.settingthisvalueto 1
; when the session.GC _ divisorvalueis 100 willgiveyouapproximatelya1% chance
; thegcwillrunonanygiverequest.increasingthisvalueto 1000 willgiveyou
; A 0.1 % chancethegcwillrunonanygiverequest.forhighvolumeproductionservers、
; this is a more efficient approach。
; Default Value: 100
; 开发价值: 1000
; Production Value: 1000
; http://PHP.net/session.GC-divisor
session.gc_divisor=1000
; After this number of seconds,storeddatawillbeseenas‘garbage’and
; cleanedupbythegarbagecollectionprocess。
; 3358 PHP.net/session.GC-max lifetime
session.gc_maxlifetime=1440
www转载请在. forasp.cn上注明