我需要能够使用与 mysql 数据库中的数据进行通信的 php 代码进行定位,该文件称为“validate.php”。它的主要功能是在登录时验证是否有空字段,如果用户值为1分配一个配置文件,值为0时分配另一个配置文件
想法是“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”,加载包含所有信息的页面。
我不知道我做错了什么。 有人可以帮助我,我将不胜感激,我已经处理了好几天了。 谢谢。
最佳答案
不要打印,试试这个:
if($_SESSION['user_admin']==1){
header('location:dashboard.php');
exit;
}else{
header('location:dashboard2.php');
exit;
}
感谢 Magnus Eriksson 的建议
关于php - php 中的 session 不加载配置文件,php + mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796905/