2015-10-17 52 views
0

我有一個數據庫與表用戶。在用戶桌上,我有一個用戶名值「corny」和用戶名=「admin」。用戶可以訪問管理頁面使用會話在php

我user.php的我有這樣的代碼

if(!isset($_SESSION['username'])){ 
    header('location: index.php'); 
} 

在我的admin.php的,我有這個會議

if(!isset($_SESSION['username'])){ 
    if($_SESSION['username'] != "gft-admin"){ 
     header('location: index.php'); 
    } 
} 

所以我的問題是,從users.php我可以訪問管理員。 PHP。如果用戶不等於'admin',我應該怎麼做以避免users.php訪問admin.php。

回答

0

您當前的邏輯是說:

IF there is no username 
    THEN if the username is not gft-admin 
    THEN REDIRECT 

但是想要說的是:

IF there is no username 
OR if the username is not gft-admin 
    THEN redirect 

,你會表達這樣的:

if(!isset($_SESSION['username']) || $_SESSION['username'] != "gft-admin"){ 
    header('location: index.php'); 
} 
0

刪除第二段代碼中的!,並在沒有$_SESSION['username']集時添加else語句。

if(isset($_SESSION['username'])){ //remove here 
    if($_SESSION['username'] != "gft-admin"){ 
     header('location: index.php'); 
    } 
} else { 
    header('location: index.php'); 
} 
+0

還是一樣,我可以訪問當我嘗試瀏覽器admin.php –

+0

@akirayuki更新了答案,用else語句再試一次。 – Thaillie

0

這是因爲您完全繞過管理if區塊已完成設置的會話。我認爲你只需要刪除!標記。

+0

仍然是我可以訪問當我trype瀏覽器admin.php –

+0

@akirayuki嘗試清除您的瀏覽器/會話緩存第一。 –