1

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();
}
user582065
  • 337
  • 4
  • 5
  • 12
  • 1
    Damn Visual Studio! Can't believe I just tried right-clicking `DURATION` in your code snippet and tried to select "Go to Definition". lol – SE13013 Jun 30 '15 at 08:55

2 Answers2

5

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();
}
?>
pinaldesai
  • 1,835
  • 2
  • 13
  • 22
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";
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62