2017-07-29 129 views
0

如何根據他們的角色重定向兩個用戶?我有數據庫用戶類型1和類型0.我的登錄代碼是這樣的。類型1假設爲管理員,類型0是普通用戶。如何根據其角色在PHP中重定向兩個用戶

<?php 

session_start(); 

if(isset($_POST['submit'])){ 

include 'dbh.inc.php'; 

$uid = mysqli_real_escape_string($conn, $_POST['uid']); 
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']); 


if (empty($uid) || empty($pwd)){ 
    header("Location: ../index.php?login=empty"); 
    exit(); 
} else { 
    $sql = "SELECT * FROM users WHERE user_uid='$uid'"; 
    $result = mysqli_query($conn, $sql); 
    $resultCheck = mysqli_num_rows($result); 
    if ($resultCheck < 1) { 
     header("Location: ../index.php?login=error"); 
     exit(); 
    } else { 
     if ($row = mysqli_fetch_assoc($result)){ 
      $hashedPwdCheck = password_verify($pwd, $row['user_pwd']); 
      if($hashedPwdCheck == false){ 
       header("Location: ../index.php?login=error"); 
       exit(); 
      } elseif ($hashedPwdCheck == true) { 
       $_SESSION['u_id'] = $row['user_id']; 
       $_SESSION['u_first'] = $row['user_first']; 
       $_SESSION['u_last'] = $row['user_last']; 
       $_SESSION['u_uid'] = $row['user_uid']; 
       header("Location: ../main.php?login=success"); 
       exit(); 
      } 
     } 
    } 
} 
} else { 
header("Location: ../index.php?login=error"); 
exit(); 
} 
+0

對於用戶1型和0,使如果別的頭條件等 標題( 「位置:../main.php?login=success」);如果(type == 1){header(「Location:../ main.php?login = success」);} elseif(type == 0){header(「Location:../mainuser.php?login=成功「);} – LogicBlower

+0

那麼你打算將管理員重定向到一個頁面,並將一個普通用戶重定向到另一個頁面? – Vagabond

+0

用戶類型是否存儲在用戶表中。我沒看到這個領域? – Progrock

回答

-1

不能僅僅通過查詢來檢查用戶類型是1還是0?

while ($row = mysqli_fetch_assoc($result)) { 
    $type = $row["yourusertypeid"]; 
} 

if ($type == 1) { 
    //user is admin 
} else if ($type == 1) { 
    //user is user 
} 
+0

檢查您的代碼。 – Progrock

0

,你可以很容易地通過if()..else...但在所有這樣做不直接使用header重定向,我建議你使用這個功能來重定向:

function redirect($url){ 
    if (headers_sent()){ 
     die('<script type="text/javascript">window.location.href=\'' . $url . '\';</script>'); 
    }else{ 
     header('Location: ' . $url); 
     die(); 
    } 
} 

這就是你需要做的:

if($type == 1) 
    redirect($admin_url); 
else 
    redirect($user_url); 
相關問題