2014-10-08 96 views
-2

以下代碼用於我的管理登錄頁面,該頁面在成功登錄後重定向到僅管理頁面。通過數據庫值,我只需要管理員值爲1的用戶被授予訪問權限,而管理員值爲0的用戶會收到類似於「無效的用戶名或密碼」的錯誤消息。請表示放置其他代碼。使用數據庫的PHP用戶/管理員登錄

另外,請記住,我將代碼和補丁放在一起,儘可能爲我工作,所以我不太流利的PHP。任何幫助將非常感激。

DATABASE 
ID Username Password Admin 
1  John  ••••••  0 
2  Aaron  ••••••  1 

<?php 

    require("connect.php"); 

    // Re-display the username if they fail to enter correct password. 
    $submitted_username = ''; 

    // Determine whether the login form has been submitted 
    // If it has, run the login code, otherwise display form 
    if(!empty($_POST)) 
    { 
     // Retrieve the users info from the database using username 
     $query = " 
      SELECT 
       id, 
       username, 
       password, 
       salt, 
       email, 
       admin 
      FROM users 
      WHERE 
       username = :username 
     "; 

     // The parameter values 
     $query_params = array( 
      ':username' => $_POST['username'] 
     );  

     try 
     { 
      // Execute query against database 
      $stmt = $db->prepare($query); 
      $result = $stmt->execute($query_params); 
     } 


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

     $login_ok = false; 

     // Retrieve user data from database. If $row is false, username in not registered 
     $row = $stmt->fetch(); 
     if($row) 
     { 
      // Using the password submitted by the user and the salt stored in the database, 
      // we now check to see whether the passwords match by hashing the submitted password 
      // and comparing it to the hashed version already stored in the database. 
      $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']) 
      { 
       // If they do, then we flip this to true 
       $login_ok = true; 
      }   
     } 

     // If the user logged in successfully, then we send them to the private members-only page 
     // Otherwise, we display a login failed message and show the login form again 
     if($login_ok) 
     { 
      // Here I am preparing to store the $row array into the $_SESSION by 
      // removing the salt and password values from it. Although $_SESSION is 
      // stored on the server-side, there is no reason to store sensitive values 
      // in it unless you have to. Thus, it is best practice to remove these 
      // sensitive values first. 
      unset($row['salt']); 
      unset($row['password']); 

      // This stores the user's data into the session at the index 'user'. 
      // We will check this index on the private members-only page to determine whether 
      // or not the user is logged in. We can also use it to retrieve 
      // the user's details. 
      $_SESSION['user'] = $row; 

      // Redirect the user to the private members-only page. 
      header("Location: index.php"); 
      die("Redirecting to: index.php"); 
     } 
     else { 
      // Tell the user they failed 
      $error = "Invalid Username or Password"; 

      // Show them their username again so all they have to do is enter a new 
      // password. The use of htmlentities prevents XSS attacks. You should 
      // always use htmlentities on user submitted values before displaying them 
      // to any users (including the user that submitted them). For more information: 
      // http://en.wikipedia.org/wiki/XSS_attack 
      $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); 
     } 
    } 

?> 
+0

你需要解釋的wh在使用當前代碼時遇到的問題,理想情況下可以重現問題的[最短代碼](http://stackoverflow.com/help/mcve)。 – parchment 2014-10-08 06:32:57

+0

我不一定對代碼有任何「問題」。就目前的工作而言,它工作得很好,我只需要額外的代碼來挑選admin 0或1的數據庫值,並僅授予管理員訪問權限。 – 2014-10-08 06:41:03

回答

0

也許這將工作: -

if($check_password === $row['password'] && $row['admin'] == 1) 
{ 
$login_ok = 1; 
}else 
{ 
$login_ok = 0; 
} 

OR

你可以改變你的查詢

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

感謝您的回覆,似乎拒絕所有用戶和管理員訪問。 – 2014-10-08 06:55:00

+0

好的,現在試試,早些時候我在管理員中使用了大寫A. :P – 2014-10-08 06:56:21

+0

就像一個魅力!非常感謝你 – 2014-10-08 07:03:31