如何在浏览器关闭或延长一段时间后让 PHP 会话过期 [英] How to have PHP session expire upon browser close OR some extended duration of time
问题描述
我的 php 会话设置为在用户关闭浏览器时过期,但我注意到如果我将浏览器打开一段时间(例如 24 小时以上),会话仍然存在.
My php session is set to expire when user closes the browser but I noticed that if I leave my browser open for an extended period of time (24+ hours for example) the session still persists.
有没有办法让这些会话在浏览器关闭或经过一段时间后过期?
Is there a way that I can have these sessions expire either when the browser is closed or when some extended period of time has transpired?
推荐答案
解决方案可能是通过 ini_set('session.gc_maxlifetime',
当然如果可以通过PHP更改配置.否则,您需要在 php.ini
中设置正确的值:
The solution might be to set data via ini_set('session.gc_maxlifetime', <lifetime in seconds>);
Of course if it's possible to change config via PHP. Otherwise you wil need to set proper values in your php.ini
:
ini_set(‘session.gc_maxlifetime’,30);
ini_set(‘session.gc_probability’,1);
ini_set(‘session.gc_divisor’,1);
另一种方法是通过以下方式设置会话 cookie 的有效性:
The other approach is setting validity of session cookie by:
$expire=24*60*60;
session_set_cookie_params($expire);
session_start();
这篇关于如何在浏览器关闭或延长一段时间后让 PHP 会话过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!