2014-09-30 52 views
0

我只是在登錄配置中與會話混淆。相關會話聲明

例如:

 if($ensure_credentials) { 
     $_SESSION['status'] = 'authorized'; 
     $_SESSION['username'] = $username; /* save the users username to session */ 
     header("location: index.php"); 
    } else return "Please enter a correct username and password"; 

在那裏,如果登錄成功,從而使會話狀態授權聲明它。但是,爲什麼在索引頁,現聲明如下:

 session_start(); 
    if($_SESSION['status'] !='authorized') header("location: login.php"); 

它意味着,如果會話狀態被授權比將被引導到登錄頁面。

我不得不問這個問題,因爲它總是引導我回到登錄頁面。

回答

0

無論何時您要寫入PHP會話或從中讀取數據,您都需要確保會話已啓動。要做到這一點,你可以使用下面的代碼:

if(!isset($_SESSION)){ 
    session_start(); 
} 

要找出您的會話目前持有你可以做:

print_r($_SESSION); 

這會告訴你什麼是當前存儲在當前會話中。

然後你做:

if($ensure_credentials) { 
    $_SESSION['status'] = 'authorized'; 
    $_SESSION['username'] = $username; 
    header("location: index.php"); 
} 

if($_SESSION['status'] !== 'authorized'){ 
    header("location: login.php"); 
}