php 中的会话不加载配置文件,php + mysql

Sessions in php do not load profiles, php + mysql

我需要能够使用 php 代码进行定位,该代码与来自 mysql 数据库的数据进行通信,该文件称为 "validate.php"。其主要功能是在table"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" and the text "dashboard" on the page, But, if I write in the browser "http://localhost/proyect/dashboard.php”加载页面包含所有信息。

我不知道我做错了什么。 有人可以帮助我,我将不胜感激,我已经处理了好几天了。 谢谢。

你需要重定向而不是回显 php 文件的内容 还要检查 { 因为还有额外的

if($_SESSION['user_admin']==1){
       header("Location: dashboard.php");
       }else{
        header("Location: dashboard2.php");
     }

不要打印,试试这个:

if($_SESSION['user_admin']==1){
       header('location:dashboard.php');
       exit;
}else{
       header('location:dashboard2.php');
       exit;
}

感谢 Magnus Eriksson 的建议