2012-12-18 39 views
1

我想顯示不同的內容,如果用戶是管理員(2),或noarmal用戶(1)。登錄工作正常,但我不知道如果用戶有'usertype'1或2熱檢查?我想使用PHP $ _SESSION。

這是我的登錄表單:

<!-- Log in --> 
<form method="post" action="authentication.php"> 
    <input type="text" name="userid" placeholder="E-mail" required/> 
    <input type="password" name="password" placeholder="Password" required/><br> 
    <button type="submit" name="submit" class="btn">Log in</button> 
</form> 

這是我authentication.php:

session_start(); 
if(isset($_POST['userid']) && isset($_POST['password'])){ 
$userid  = $_POST['userid']; 
$password = $_POST['password']; 

$dbc  = mysqli_connect('localhost', 'root', '', 'news'); 
if(!$dbc){ 
echo "Cannot connect to database"; 
exit; 
} 

$query  = "SELECT * FROM users_tbl WHERE email = '$userid' AND password = sha1('$password')"; 
$result  = $dbc->query($query); 
if($result->num_rows) 
{ 
$_SESSION['valid_user'] = $userid; 
} 
$dbc->close(); 
} 
if(isset($_SESSION['valid_user'])){ 
header("location:prefs.php"); 
}else{ 
header("location:login.php"); 
echo 'Error'; 
} 

這是我曾嘗試:

<?php 

    mysql_connect('localhost', 'root', ''); 
    mysql_select_db('news'); 

    $sql = "SELECT users_tbl.usertype FROM users_tbl"; 
    $result = mysql_query($sql); 
    $user = mysql_fetch_array($result); 
    $_SESSION['user'] = $user['user']; 
    $_SESSION['usertype'] = $user['usertype'];   

    if($_SESSION['user']['usertype'] == 2) 
    {?> 
    <h1>Only admin stuff</h1> 

    <$? }//Endif user is admin(2) ?> 

也許不是做的查詢每次檢查用戶是否爲admin,我可以保存一個$ _SESSION ['usertype'],然後用它來檢查用戶是否爲2,爲管理員,也許當用戶登錄?但我不知道該怎麼做。我對此很新穎。

+1

你需要調用'在session_start()'你開始使用'$ _SESSION'瓦爾之前,這可能是一個原因,它不設置它們。 – Stu

+0

session_start()位於每一頁的頂部,用於檢查$ _SESSION ['valid_user']是否已設置 – user1906437

回答

0

試試這個

if($_SESSION['usertype'] == 2) 
{ 
    //do stuff here 
} 
if ($_SESSION['usertype']) == 1) 

{ 
//do stuff here 
} 

編輯: 如果你不想寫那麼多東西,你只想包括管理裏面的東西的用戶這一塊只是這樣做

if ($_SESSION['usertype']) == 1 or $_SESSION['usertype']) == 2) 

    { 
       //do stuff here for them both admin and users 
     if($_SESSION['usertype'] == 2) 
            { 
            //do extra stuff here for only admin 
            } 
    } 
+0

我試過這個,但是當我以管理員或普通用戶身份登錄時,它的內容是相同的? – user1906437

+0

不只是做不同的內容,如果,如果管理員,他爲他寫的東西和相同的用戶 –

+0

看我更新的答案,如果你不想爲他們寫雙重的東西,只包括額外的員工爲管理 –

0

您可以保存與會話變量用戶類型

登錄後,你需要檢查與數據庫與用戶類型1或2,存儲與像$ _SE會話變量,變量裂變[ 'USER_TYPE']。

因此,基於您可以跟蹤。

+0

我怎麼能檢查這個? – user1906437

0

嘗試使用用戶類型該表添加一列。 從表格中提取數據時檢查該列的值。如果值爲admin,則不顯示文本。

2

我意識到這是一個老問題,但我花了上週搜索互聯網尋找一個回答這個問題,因爲我也有過類似的問題。 「echo_Me」有答案,但我看到一些不正確的語法。在本地服務器上玩弄它後,這就是我所做的。

if($_SESSION['usertype']) == 1 or $_SESSION['usertype']) ==2) 
//> start   end <       end < < end 
{ 
    //do stuff here for them both admin and users 
    if($_SESSION['usertype'] == 2) 
    { 
     //Do other stuff for usertype 2 
    } 
    } 

你有更多的結束括號,然後你開始做。 當我爲我的網站運行登錄腳本時,我設置了所有需要的會話變​​量。所以,如果我需要一個會話變量,這裏是我將如何處理它:

if($_SESSION['user']['usertype'] == 1 OR $_SESSION['user']['usertype'] == 2) { ?> 
// Do Stuff for normal user and admin 
    <?php } if($_SESSION['user']['usertype'] == 2) { ?> 
     // DO Stuff for admin only 
    <?php } ?> 

正如你可以看到,第一個if語句只有一組括號。實際上,您可以刪除一些php代碼,只需執行常規html/css標記,然後使用forrtype的2用於管理員的if語句。

<!-- HTML Markup for both Admin and Regular Users Goes Here above if statement --> 
<?php if($_SESSION['user']['usertype'] == 2 { ?> 
    // Markup for Admin only 
<?php } ?> 

此外,我會建議沒有在會話變量中設置密碼。在那段代碼中,你可能沒有這樣做,我只是不熟悉mysqli,因爲我使用PDO。

相關問題