【问题标题】:PHP Expire Session after 5 minutes5 分钟后的 PHP 过期会话
【发布时间】:2011-10-21 06:20:06
【问题描述】:

我有以下代码在设定的时间后使会话过期。但是,它无法正常工作。如果我将它设置为例如 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();
}

【问题讨论】:

  • 该死的 Visual Studio!不敢相信我刚刚尝试在您的代码 sn-p 中右键单击 DURATION 并尝试选择“转到定义”。大声笑

标签: php session


【解决方案1】:

稍微修改一下代码看看。这应该可以。

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

【讨论】:

    【解决方案2】:
    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";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-15
      • 2017-05-17
      • 2010-10-05
      • 2015-04-15
      • 2013-01-27
      相关资源
      最近更新 更多