根据用户角色

时间:2016-04-24 18:13:40

标签: php

我目前正在为客户创建一个网站,他希望在您登录时拥有所有订阅者的页面和管理员页面。现在我有一个如下所示的登录脚本:

while($row = mysqli_fetch_array($select_user_query)) {
        $db_user_id = $row['user_id'];
        $db_user_email = $row['user_email'];
        $db_user_password = $row['user_password'];
        $db_user_firstname = $row['user_firstname'];
        $db_user_lastname = $row['user_lastname'];
        $db_user_role = $row['user_role'];
    }
    $password = crypt($password, $db_user_password);
    if($user_email === $db_user_email && $password === $db_user_password ) {
        $_SESSION['user_email'] = $db_user_email;
        $_SESSION['user_firstname'] = $db_user_firstname;
        $_SESSION['user_lastname'] = $db_user_lastname;
        $_SESSION['user_password'] = $db_user_password;
        $_SESSION['user_role'] = $db_user_role;
        $_SESSION['user_id'] = $db_user_id;
        header("Location: ../admin ");
    } 
    else {
        header("Location: ../index.php ");
    }   
}

但我想创建一个脚本,如果他的$ user_role是订阅者,则将用户发送给订阅者,如果他是管理员,则发送给管理员。

2 个答案:

答案 0 :(得分:1)

如果我了解您的情况,解决方案是更改IF的结尾部分:

while($row = mysqli_fetch_array($select_user_query)) {
    $db_user_id = $row['user_id'];
    $db_user_email = $row['user_email'];
    $db_user_password = $row['user_password'];
    $db_user_firstname = $row['user_firstname'];
    $db_user_lastname = $row['user_lastname'];
    $db_user_role = $row['user_role'];
}

$password = crypt($password, $db_user_password);
if($user_email === $db_user_email && $password === $db_user_password ) {
    $_SESSION['user_email'] = $db_user_email;
    $_SESSION['user_firstname'] = $db_user_firstname;
    $_SESSION['user_lastname'] = $db_user_lastname;
    $_SESSION['user_password'] = $db_user_password;
    $_SESSION['user_role'] = $db_user_role;
    $_SESSION['user_id'] = $db_user_id;
    if($db_user_role == 'admin') header("Location: ../admin");
    elseif($db_user_role == 'subscriber') header("Location: ../subscriber");
    die();
} 

header("Location: ../index.php ");

答案 1 :(得分:0)

我尝试在if语句中创建if语句,检查凭据是否正确。我做了这个if语句:

<?php
if(isset($_SESSION['user_role'])) { 
if(isset($_SESSION['user_role']) == 'admin') 
{ 
header("Location: ../admin"); 
} else if(isset($_SESSION['user_role']) == 'subscriber')
{ 
header("Location: ../subscriber"); 
} 
} 
} else { 
header("Location: ../index.php ");
 } 
}

&GT;