PHP 会话在 5 分钟后过期

发布于 2024-12-11 09:38:44 字数 475 浏览 0 评论 0原文

我有以下代码可以在设定的时间后使会话过期。然而它无法正常工作。如果我将其设置为例如 1 分钟甚至 5 分钟,它会立即过期。你能帮忙吗?

// duration in minutes * seconds
$duration = (DURATION * 60);

if(isset($_SESSION['started']))
{
    // show banner and hide form
    echo $msg;
    $showform = 0;
    if((time() - $_SESSION['started'] - $duration) > 0)
    {
        unset($_SESSION['count']);
        unset($_SESSION['offender']);
        $showform = 1;
    }
}
else
{
  $_SESSION['started'] = time();
}

I have the following code to expire a session after set amount of time. It is however not working properly. If I set it for example for 1 minute or even 5 minute, it expires immediately. Can you help?

// duration in minutes * seconds
$duration = (DURATION * 60);

if(isset($_SESSION['started']))
{
    // show banner and hide form
    echo $msg;
    $showform = 0;
    if((time() - $_SESSION['started'] - $duration) > 0)
    {
        unset($_SESSION['count']);
        unset($_SESSION['offender']);
        $showform = 1;
    }
}
else
{
  $_SESSION['started'] = time();
}

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

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

发布评论

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

评论(2

月光色 2024-12-18 09:38:44

稍微修改一下你的代码看看。这应该有效。

<?php
session_start();
$duration = (DURATION * 60);

if(isset($_SESSION['started']))
{
    // show banner and hide form

    $showform = 0;
    $time = ($duration - (time() - $_SESSION['started']));
    if($time <= 0)
    {
        unset($_SESSION['count']);
        unset($_SESSION['offender']);
        $showform == 1;
    }
}
else
{
  $_SESSION['started'] = time();
}
?>

Modified your code little bit take a look. This should work.

<?php
session_start();
$duration = (DURATION * 60);

if(isset($_SESSION['started']))
{
    // show banner and hide form

    $showform = 0;
    $time = ($duration - (time() - $_SESSION['started']));
    if($time <= 0)
    {
        unset($_SESSION['count']);
        unset($_SESSION['offender']);
        $showform == 1;
    }
}
else
{
  $_SESSION['started'] = time();
}
?>
幽蝶幻影 2024-12-18 09:38:44
session_cache_expire(5);
$cache_expire = session_cache_expire();
session_start();
echo "The cache limiter is now set to $cache_limiter<br />";
echo "The cached session pages expire after $cache_expire minutes";
session_cache_expire(5);
$cache_expire = session_cache_expire();
session_start();
echo "The cache limiter is now set to $cache_limiter<br />";
echo "The cached session pages expire after $cache_expire minutes";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文