I have an index.php page that a session is set inside($_SESSION['expire']). This session should be unset after 30 mins and we should redirect to index.php (to verify the user again).
some part of my index.php code:
<?php
session_start();
//if user name and password are valid do the following:
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ;
?>
<a href="index.php?action=ContentManager">
content
</a>
<?php
if(isset($_REQUEST['action']))
{
//if the expiration time has not reached yet do the following
$now=time();
if (isset($_SESSION['expire']) && ($now<= $_SESSION['expire']))
{
switch($_REQUEST['action'])
{
case 'ContentManager' :
include('model/content.php');
$contents = getContent($conn, ' where 1=1');
include('view/contentmanager.php');
break;
}
}
else if($now > $_SESSION['expire'])
{
unset($_SESSION['expire']);
session_destroy();
header('location:index.php');
exit();
}
}
?>
the problem is that when I click contentmanager link after 30 mins, we will redirect to an empty page with url: index.php?action=contentmanager
And only if I refresh the page again, we will redirect to index.php itself and the login form will be appeared.
So breifly: I have to refresh the page two times to redirect to the correct page.
Thanks in advance
use ob_start();
<?php
session_start();
ob_start();
//if user name and password are valid do the following:
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ;
?>
<a href="index.php?action=ContentManager">
content
</a>
<?php
if(isset($_REQUEST['action']))
{
//if the expiration time has not reached yet do the following
$now=time();
if (isset($_SESSION['expire']) && ($now<= $_SESSION['expire']))
{
switch($_REQUEST['action'])
{
case 'ContentManager' :
include('model/content.php');
$contents = getContent($conn, ' where 1=1');
include('view/contentmanager.php');
break;
}
}
else if($now > $_SESSION['expire'])
{
unset($_SESSION['expire']);
session_destroy();
header('location:index.php');
exit();
}
}
ob_end_flush();
?>