5分钟后PHP过期会话

时间:2011-10-21 06:20:06

标签: php session

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

2 个答案:

答案 0 :(得分:2)

修改你的代码一点看看。这应该有用。

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

答案 1 :(得分:1)

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";