由用户设置 php 会话超时 [英] set php session timeout by user

查看:43
本文介绍了由用户设置 php 会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在 php 中由用户设置会话超时吗?

It´s possible to set session timeout by user in php?

示例:2 个用户在我的网站中注册.我希望每个用户都可以设置自己的会话超时.

Example: 2 users are registred in my site. I want that each user can set their own session timeout.

推荐答案

是的,您可以为每个用户设置自定义会话超时.您可以使用 中所述的方法如何在 30 分钟后使 PHP 会话过期? 但要存储绝对过期时间:

Yes, you can set a custom session timeout for each user. You can use the method as described in How do I expire a PHP session after 30 minutes? but store the absolute expiration time instead:

// set expiration time
$_SESSION['EXPIRES'] = time() + $customSessionLifetime;

// validate session
if (isset($_SESSION['EXPIRES']) && (time() < $_SESSION['EXPIRES'])) {
    // session still valid; update expiration time
    $_SESSION['EXPIRES'] = time() + $customSessionLifetime;
} else {
    // session invalid or expired
    session_destroy();
    session_unset();
}

这里 $customSessionLifetime 可以为每个用户设置不同的.只需确保其值小于或等于 session.gc_maxlifetimesession.cookie_lifetime(如果您使用 cookie 作为会话 ID).

Here $customSessionLifetime can be set differently for each user. Just make sure that its value is less than or equal to session.gc_maxlifetime and session.cookie_lifetime (if you use a cookie for the session ID).

这篇关于由用户设置 php 会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆