【问题标题】:php redirect both roles to same pagephp将两个角色重定向到同一页面
【发布时间】:2021-01-04 22:29:16
【问题描述】:

有两种角色,管理员角色和用户角色。每个用户都必须被重定向到不同的页面,但两者都被重定向到 adminsitrador 角色的页面。

index.php 包含登录表单

 <?php

include('conexion.php');

?>
<!DOCTYPE html>
<html lang="zxx">

<head>
<title>Inicio de Sesion</title>
<!-- Meta-Tags -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta name="keywords" content="Core Login Form a Responsive Web Template, Bootstrap Web Templates, Flat Web Templates, Android Compatible Web Template, Smartphone Compatible Web Template, Free Webdesigns for Nokia, Samsung, LG, Sony Ericsson, Motorola Web Design">
<!-- //Meta-Tags -->
<!-- Index-Page-CSS -->
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<!-- //Custom-Stylesheet-Links -->
<!--fonts -->
<link href="//fonts.googleapis.com/css?family=Mukta+Mahee:200,300,400,500,600,700,800" rel="stylesheet">
<!-- //fonts -->
<!-- Font-Awesome-File -->
<link rel="stylesheet" href="css/font-awesome.css" type="text/css" media="all">
</head>

<body>
<h1 class="title-agile text-center">Acceder al Sistema</h1>
<div class="content-w3ls">
    <div class="agileits-grid">
        <div class="content-top-agile">
            <h2>Datos de Ingreso</h2>
        </div>
        <div class="content-bottom">
            <form action="validacion.php" method="post">
                <div class="field_w3ls">
                    <div class="field-group">
                        <input name="user" id="user" type="text" value="" placeholder="User" required>
                    </div>
                    <div class="field-group">
                        <input id="passw" type="passw" class="form-control" name="passw" value="" placeholder="Password">
                        <span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
                    </div>
                </div>
                <div class="wthree-field">
                    <input id="saveForm" name="saveForm" type="submit" value="Login" />
                </div>
            </form>
        </div>
        <!-- //content bottom -->
    </div>
</div>
<div class="copyright text-center">
    <p>© 2020 Management documents
    </p>
</div>
<!--//copyright-->

</body>
<!-- //Body -->

</html>

validation.php 验证用户并重定向到角色对应的页面。

<?php

include('conexion.php');


$user = addslashes($_POST['user']);
$passw = addslashes($_POST['passw']);

#
$query_user = mysqli_query($conexion,"SELECT * FROM login WHERE user = '".$user."'");
    $query_role = mysqli_query($conexion,"SELECT role FROM login WHERE user = '".$user."' ");
if(mysqli_num_rows($query_user) < 1){
    echo "<script> alert('Error en datos...');</script>";
    echo "<script>document.location.href = 'index.php'; </script>";
}else{
    $query_passw = mysqli_query($conexion,"SELECT * FROM login WHERE user = '".$user."' AND password = '".$passw."' ");
    if (mysqli_num_rows($query_passw) < 1) {
        echo "<script> alert('Date Error...');</script>";
        echo "<script>document.location.href = 'index.php'; </script>";
    }else{

        echo "<script> alert('SucIngreso Exitoso...');</script>";
        $role = mysqli_fetch_assoc($query_role);                         
                    
        if(strcmp($role,"admin") == 0){
                        header('Location: admin.php');
                        
        }
        elseif(strcmp($role,"user") == 0){
                      header('Location: user.php');
        }
    }
}


?>

进行查询以了解与启动会话的用户对应的角色并验证角色以将其重定向到相应的页面,但两个用户都被重定向到管理员角色的页面。

【问题讨论】:

  • strcmp($role,"admin")改成strcmp($role["role"],"admin"),因为mysqli_fetch_assoc返回一个关联数组
  • 你需要参考一些教程。这里使用了许多不好的做法。您的fetch 用法也意味着您还没有看到它是如何返回的。 php.net/manual/en/mysqli-result.fetch-assoc.php 也不需要相同的查询 3 次,$consultar_user 似乎永远不会设置。

标签: php mysql redirect mysqli roles


【解决方案1】:

我将给你一个在 php 中快速调试的通用方法 这是var_dump()函数的使用,它用来显示变量的类型和值,用在看起来有错误行为的变量上。

在这种情况下 因为它总是重定向到页面'admin.php', 这意味着这个陈述总是正确的 strcmp($role,"admin") == 0 因此,使用var_dump($role); 检查变量值以查看它在每次运行时的值,可能是数据库中的值始终为“admin”,因此您需要检查它。 并使用if($role === "admin") 而不是if(strcmp($role,"admin") == 0) 它更清晰

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多