登录后根据用户角色重定向到其他页面 [英] After login redirect to different page according to user role

查看:89
本文介绍了登录后根据用户角色重定向到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改代码以在登录后根据用户角色重定向到不同页面,无论角色如何,我都只能进行一次通用重定向.我需要有关如何实施的帮助.

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

我的PHP登录代码在这里:

My PHP login Code is here:

<?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 i.e. either

普通用户为"N",公司用户为"C".

"N" for normal user OR "C" for company user.

推荐答案

所以,让我们开始吧:

$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
}

这篇关于登录后根据用户角色重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆