【发布时间】: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