【发布时间】:2017-05-05 05:06:09
【问题描述】:
我需要能够使用与 mysql 数据库中的数据进行通信的 php 代码进行定向,该文件称为“validate.php”。它的主要功能是验证登录时是否有空字段,如果用户在表“users”的记录中值为1,则分配一个profile,为0时分配另一个profile
这个想法是“validate.php”检查用户并根据他们的个人资料将其引导到一个页面,但我不能这样做。
我的代码是:
<?php
require('access_db.php');
session_start();
if(isset($_POST['send'])) { // We verify that the form data has been sent
//We verify that the user_name and the user_pass fields are not empty
if(empty($_POST['user_name']) || empty($_POST['user_pass'])) {
echo"
<script>
alert('Please enter your username and password correctly ');
location.replace('login.php');
</script>
";
}else {
//"Clean" the form fields of possible malicious code
$user_name = mysqli_real_escape_string($link,trim($_POST['user_name']));
$user_pass = mysqli_real_escape_string($link,trim($_POST['user_pass']));
// We verify that the data entered in the form match those of the DB
$query=mysqli_query($link,"select user_id,user_name,user_admin FROM users WHERE user_name='".$user_name."' and user_pass ='".$user_pass."'");
$row = mysqli_fetch_array($query);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_name'] = $row["user_name"];
$_SESSION['user_admin'] = $row["user_admin"];
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
{
}
}else{
header("Location: login.php");
}?>
我的主要问题在这里:
if($_SESSION['user_admin']==1){
echo "dashboard.php";
}else{
echo "dashboard2.php";
}
当我在我的页面“login.php”中使用管理员用户登录时,您应该检查信息并根据您的个人资料转到一个页面,该页面仅显示在浏览器“http://localhost/proyect/validate.php”和文本“仪表板”上页面,但是,如果我在浏览器中写入“http://localhost/proyect/dashboard.php”,则加载包含所有信息的页面。
我不知道我做错了什么。 有人可以帮助我,我将非常感激,我已经做了好几天了。 谢谢。
【问题讨论】:
-
为什么要重定向它们时要在屏幕上打印。
-
您的代码容易受到 SQL 注入的影响 - 使用准备好的语句而不是直接在 sql 中嵌入变量
-
@programmingArrow,你好,我的想法不是打印信息,正如你所说,我想通过个人资料重定向到我的页面,感谢 Magnus Eriksson
-
@MagnusEriksson,非常感谢您的建议!!!我正在学习编程,我会犯错误,但感谢社区,我会学到更多。
-
@RamRaider,我是新手,非常感谢,我会了解你提到的。谢谢!!
标签: php mysql session mysqli profile