0

我有一个 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?action=contentmanager

只有当我再次刷新页面时,我们才会重定向到 index.php 本身并出现登录表单。

简而言之:我必须刷新页面两次才能重定向到正确的页面。

提前致谢

4

1 回答 1

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();
?>
于 2013-02-09T20:46:27.043 回答