2016-03-14 92 views
0

**此代碼使成功登錄的用戶可以被定向到student_profile.php頁面,但是我需要這個來檢查嘗試登錄的用戶的類型,並根據以將該成員發送到適當的位置。在DB中,我有一個叫做account的字段,它具有不同的成員類型;學生,房東和管理員,他們都有自己的網頁。 **如果else或開關的情況下從登錄頁面的不同位置爲不同的成員組

<?php 
    include ("connect.php"); 

    if (isset($_POST["user_login"]) && isset ($_POST["user_pass"])){ 
     // formatting field via reg replace to ensure email and password only conisists of letters and numbers preg_replace('#[^A-Za-z0-9]#i','', 
     $login_user = $_POST["user_login"]; 
     $login_password = $_POST["user_pass"]; 


     // password is encryted in DB (MD5) therefore user inputted password will not match encryted password in DB - we have to assign new var 
     $decrypted_password = md5($login_password); 

    // Query which finds user (if valid) from DB - Achieving authentication via username and password  

    $user_query = mysqli_query($connect, "SELECT * FROM users WHERE email = '$login_user' AND password = '$decrypted_password' AND closed = 'no' LIMIT 1"); 

     $check_user = mysqli_num_rows($user_query); // checking to see if there is infact a user which those credentials in the DB 
      if ($check_user==1){ 

      while ($row = mysqli_fetch_array($user_query)){ 
        $id = $row['user_id']; 
       } 
       // if the user credentials are correct, log the user in: 
       $_SESSION["user_login"] = $login_user; 
        header("Location:profile_student.php"); // refresh page 
       exit; 
      } 
      else { 
       echo "<div class='wrong_login'> 
          <p> Email or password is incorrect, please try again. </p> 
         </div>"; 

      } 
    } 

     ?> 

回答

0

用途:

if ($check_user==1){ 

     while ($row = mysqli_fetch_array($user_query)){ 
       $id = $row['user_id']; 
       $user_type = $row['account']; 
      } 
      // if the user credentials are correct, log the user in: 
      $_SESSION["user_login"] = $login_user; 
      // check the user type and redirect according to it 
      if($user_type == "student"){ 
      $redirection_page = "student.php"; 
      } elseif ($user_type == "Landlord"){ 
       $redirection_page = "landlord.php"; 
      } elseif ($user_type == "Administrator"){ 
       $redirection_page = "administrator.php"; 
      } else { 
       $redirection_page = "default.php"; 
      } 
       header("Location:{$redirection_page }"); // refresh page 
      exit; 
     } 
+0

感謝這就是偉大的@Ravinder雷迪 –

+0

我想這一段代碼來顯示一個錯誤,如果用戶沒有輸入正確的電子郵件或密碼。它似乎工作,但現在不工作。 –

相關問題