简体   繁体   中英

php session timeout in loop?

How do I do a loop - while the session is authorized check if the time has expired. My code looks like this:

while ($_SESSION['auth']) {
    $inactive = 600;
    if (isset($_SESSION['timeout'])) {
        $session_life = time() - $_SESSION['timeout'];
        if ($session_life > $inactive) {
            session_destroy();
            header("Location: logoutpage.php");
        }
    }
}

Wow do not do that you will get stuck in a loop for a while..

But all in all, the loop you have now should work.

Ok, I've done the following, maybe it can help someone else:

page1

session_start();
///
$teststart = 3600;
$now = time();
$testend = ($now + $teststart);
define(testend1,$testend);
$_SESSION['now2'] = $now;
$_SESSION['testend2'] = testend1;
header('Location:http://localhost/index.php?option=com_content&view=article&id=51');

page2

in the head section so that the page refreshes

<meta http-equiv="refresh" content="300">

then in page 2

session_start();
if ($_SESSION['auth']) {
$session_testend3 = $_SESSION['testend2'];
$now = time();
if ($now > $session_testend3) {
session_destroy();
header('Location: http://localhost/');
  exit;
}
}else{
echo "you are not authorized";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM