简体   繁体   English

登录后根据用户角色重定向到其他页面

[英]After login redirect to different page according to user role

I would like to modify my code to redirect to different pages after login depending on user roles, I have only been able to make one universal redirect regardless of the role. 我想修改代码以在登录后根据用户角色重定向到不同页面,无论角色如何,我都只能进行一次通用重定向。 I need help on how to implement that. 我需要有关如何实施的帮助。

Here is my db table of users: 这是我的用户数据库表:

userID  fullname    email               role    password
1       Tester one  test@gmail.com      N       ALSJALDDJ6464!JJLSK
2       Tester two  tester@gmail.com    C       @ALSJAL5656DDJJJLSK

My PHP login Code is here: 我的PHP登录代码在这里:

<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

// establishing the MySQLi connection

// Create connection
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "timewise";

$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// checking the user
if(isset($_POST['btnLogin'])){
$email = mysqli_real_escape_string($conn,$_POST['uemail']);
$pass = mysqli_real_escape_string($conn,$_POST['upass']);

//$pass = crypt($pass);


$sel_user = "SELECT * FROM users WHERE email='$email' AND pass='$pass'";

$run_user = mysqli_query($conn, $sel_user);
if (!$sel_user) {
die(mysqli_error($conn));
}

$check_user = mysqli_num_rows($run_user);

if($check_user>0){
$user_data = mysqli_fetch_assoc($run_user);

if ($user_data["role"] == 'C') {
    //redirect somehwere
    print '<script type="text/javascript">window.location = "index.html"</script>';
} else {
    //redirect somehwere else
    print '<script type="text/javascript">window.location = "form-wizard.html"</script>';
}
} else {
    // more code here
    echo '<center><span style="color:#801b1b; border:1px solid #e5a3a3; background:#ffcfcf; padding:5px;">Email or password is not correct, please try again!</span></center>';
}

}

?>

I need help on redirecting logged in users to different pages depending on the role ie either 我需要根据角色将登录用户重定向到不同页面的帮助,即

"N" for normal user OR "C" for company user. 普通用户为“ N”,公司用户为“ C”。

So, let's start: 因此,让我们开始:

$sel_user = "SELECT * FROM users WHERE email='$email' AND pass='$pass'";
$run_user = mysqli_query($conn, $sel_user);

// this will never happens as $sel_user is a string and it's not empty
// I suppose you need to check `$run_user`
if (!$sel_user) {
    die(mysqli_error($conn));
} 

$check_user = mysqli_num_rows($run_user);
if($check_user>0){
    // here you need $run_user data
    // use fetch_ methods, for example `fetch_assoc()`
    $user_data = mysqli_fetch_assoc($run_user);
    // print_r($user_data) to check keys
    if ($user_data["role"] == 'C') {
        //redirect somehwere
    } else {
        //redirect somehwere else
    }
} else {
    // more code here
}

you could use the header('location: /file') function to immediately redirect to a different page according to user position in some if/else or have the link as a variable and it would be header("location: " . $file) . 您可以使用header('location: /file')函数根据用户在某些if/else位置立即重定向到另一个页面,或者将链接作为变量,它将为header("location: " . $file) I hope that this is helpful! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM