2015-11-21 243 views
-2

我已成功創建用戶登錄,但試圖執行管理員登錄現在任何幫助,將不勝感激,到目前爲止,我有:創建用戶和管理員登錄

 $query = " 
     SELECT 
      id, 
      username, 
      password, 
      salt, 
      email, 
    firstname, 
    admin 
     FROM users 
     WHERE 
      username = :username 
    "; 


    $query_params = array(
     ':username' => $_POST['username'] 
    ); 

    try 
    { 

     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 

    } 
    catch(PDOException $ex) 
    { 
    $ex->getMessage(). 

     die("Failed to run query: " . $ex->getMessage()); 
    } 

    $login_ok = false; 
    $row = $stmt->fetch(); 
    if($row) 
    { 

     $check_password = hash('sha256', $_POST['password'] . 
     $row['salt']); 
     for($round = 0; $round < 65536; $round++) 
     { 
      $check_password = hash('sha256', $check_password . 
      $row['salt']); 
     } 

     if($check_password === $row['password']) 
     { 

      $login_ok = true; 
     } 
    } 



    if($login_ok) 
    { 
     unset($row['salt']); 
     unset($row['password']); 



    $_SESSION['user'] = $row; 
    $_SESSION['admin'] = $admin; 




     header("Location: private.php"); 
     die("Redirecting to: private.php"); 
    } 

    $admin = $row["admin"]==1; 

    if($admin){ 
    header("Location: memberlist.php"); 
    } 

    if(!$admin){ 
    header("Location: private.php"); 
    } 



    else 
    { 
     // Tell the user they failed 
     print("Login Failed."); 

在我的數據庫,我已經設置了管理員字段布爾defualt值爲1.

+1

請閱讀[**如何在此提問**](http://stackoverflow.com/questions/how-to-ask)。尋求調試幫助的問題(「**爲什麼不是這個代碼工作?」)必須包括所需的行爲,*特定的問題或錯誤*和*在問題本身中重現它*所需的最短代碼* *。沒有**明確問題陳述**的問題對其他讀者沒有用處。請參閱:[如何創建最小,完整和可驗證示例。](http://stackoverflow.com/help/mcve) –

+0

當我以管理員身份登錄時,如果管理員日誌仍然會出現在正常用戶頁面(private.php)中在我希望他們去membeslist.php。但顯然首先查詢必須知道用戶或管理員是否登錄。 – asaf

+0

我無法讓管理員登錄並進入成員列表頁面 – asaf

回答

0

我改變了字段類型的默認值admin,並添加了一條if語句,現在我有一個管理員用戶和一個普通用戶,頁面根據用戶級別重定向。

 if($_SESSION['user']['type'] == 'admin') { header("location: 
    memberlist.php"); } 

else 
    header("Location: private.php"); 
    die("Redirecting to: private.php"); 
    }