按用户设置 php 会话超时

发布于 2024-10-10 04:33:55 字数 73 浏览 0 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-10-17 04:33:55

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

// 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)。

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();
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文