2017-07-29 8 views
0

どのように2人のユーザーを自分の役割に従ってリダイレクトできますか?私はデータベースのユーザータイプ1とタイプ0を持っています。私のログインコードはこのようなものです。タイプ1は管理者であり、タイプ0は通常のユーザです。PHPでの役割に応じて2人のユーザーをリダイレクトする方法

<?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

は、 ヘッダ(「場所:../main.php?login=success」)のような場合は他のヘッダ条件を作ります。 ( "Location:../ main.php?login = success");} elseif(type == 0){header( "Location:../mainuser.php?login=成功 ");} – LogicBlower

+0

adminを1ページに、通常のユーザーを別のページにリダイレクトする予定はありますか? – Vagabond

+0

ユーザータイプはユーザーテーブルに格納されていますか。私はこのフィールドが表示されません? – Progrock

答えて

-1

ユーザータイプがクエリで1または0かどうかを確認するだけですか?

あなたが if()..else...ことにより、容易にこれを行うことができますが、すべてで、私はあなたがリダイレクトするために、この機能を使用することをお勧め、リダイレクトするために直接 headerを使用していない
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

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

これはあなたが何をする必要があるかです。ユーザタイプ1とタイプ0

if($type == 1) 
    redirect($admin_url); 
else 
    redirect($user_url); 
関連する問題