2013-02-09 42 views
0

我有一個會話在裏面設置的index.php頁面($ _ SESSION ['expire'])。此會話應在30分鐘後取消設置,我們應重定向到index.php(以再次驗證用戶)。header('location: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聯繫,我們將重定向到一個空白頁面,網址: 的index.php行動= contentmanager

只有當我再次刷新頁面時,我們將重定向到index.php本身,登錄表單將出現。

所以很簡單:我必須刷新頁面兩次以重定向到正確的頁面。直到你處理請求和所有的數據和諸如此類的東西

在此先感謝

+0

不輸出任何東西。這也將幫助您避免意大利麪代碼。 – 2013-02-09 20:48:11

回答

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(); 
?> 
+0

非常感謝CooPer。這是一個很大的幫助。現在它工作正常:) – 2013-02-09 21:03:02

+1

它更好地刪除ob_start()並檢查到期之前,在發送任何東西輸出之前,在這種情況下,在' content ' – Cooper 2013-02-09 21:06:52

相關問題