我有一个index.php页面,里面设置了一个会话($ _ SESSION ['expire'])。此会话应在30分钟后取消设置,我们应该重定向到index.php(再次验证用户)。
我的index.php代码的一部分:
<?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();
}
}
?>
问题是,当我在30分钟后点击contentmanager链接时,我们将使用url重定向到空白页面: 的index.php?行动= contentmanager
只有当我再次刷新页面时,我们才会重定向到index.php本身,并且会出现登录表单。
如此简洁:我必须刷新页面两次以重定向到正确的页面。
提前致谢
答案 0 :(得分:0)
使用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();
?>