1

I have an online system and need a user to be logged out after 60 minutes of inactivity.

Now the current code works out what 30 minutes less is by.

    $cut = time() - 30*60;

Now I would have thought the following would remove an hour so the time out is 60 minutes.

$cut = time() - 60*60;

This is a the full code that happens. After 60 minutes of inactivity I need the person to be logged out.

// Time out Users 60 minutes
    $last = $_SESSION['time'];
    $cut = time() - 60*60;

    if ($last < $cut)
        {
            session_destroy();

            $get = $_GET;
            $location = 'login.php?timeout=true';
            foreach($get as $key => $item)
                {
                    $location .= '&'.$key."=".$item;
                }
            header('Location: '.$location);
        }
    else
    $_SESSION['time'] = time();

Now that code does not work i don't think.

benomatis
  • 5,536
  • 7
  • 36
  • 59
Ryan Bowden
  • 171
  • 1
  • 12
  • Mayby this helps you how to set the lifetime of a session! http://stackoverflow.com/q/6360093/3933332 – Rizier123 Oct 10 '14 at 13:10
  • You need the person to be logged out (ie: sessions destroyed). Do you need the `?timeout=true`? If not, set the maximum session lifetime to 3600 - http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime – ʰᵈˑ Oct 10 '14 at 13:10
  • i think you need to learn about session handler class. http://php.net/manual/en/sessionhandler.destroy.php – Muhammad Oct 10 '14 at 13:11

4 Answers4

1

If you just want to see if it's been more than an hour since $_SESSION['time'] has expired, just add an hour to that time and compare it to now. If it is less than now it is more than an hour ago.

$now  = new DateTime(); 
$last = new DateTime('@' . $_SESSION['time']);
$last->modify('+1 hour');
if ($last < $now) {

}

Or subtract an hour from now and see if it is still greater than $_SESSION['time']:

$now  = new DateTime(); 
$now->modify('+1 hour');
$last = new DateTime('@' . $_SESSION['time']);
if ($last < $now) {

}
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

use strtotime() it could come handy

declare the session like this every time user makes a req.

$_SESSION['time'] = strtotime("now");

the coding part

$last = $_SESSION['time']; 

$cut = strtotime("-60 min");


if ($last < $cut)  //can also use strtotime("-60 min") directly
    {
       // session_destroy();
        unset($_SESSION['time']);  //use inorder to delete only that session


        $get = $_GET;
        $location = 'login.php?timeout=true';
        foreach($get as $key => $item)
            {
                $location .= '&'.$key."=".$item;
            }
        header('Location: '.$location);
    }
else
$_SESSION['time'] = strtotime("now");

make sure session is started in every pages.

works fine...

Ronser
  • 1,855
  • 6
  • 20
  • 38
0

You need to convert time given as below

$cut = time();
$lesstime = date('YOUR TIME FORMATE',strtotime($cut,"-1 hours"));
ujash joshi
  • 307
  • 1
  • 14
0

Try with below code:

$cut = time() - 60*60;

replace with below

$cut = strtotime('-1 hour');
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28