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

时间:2017-05-05 05:06:09

标签: php mysql session mysqli profile

我需要能够使用与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";
 }

当我在我的页面中使用我的管理员用户登录&#34; login.php&#34;您应该检查信息并根据您的个人资料转到某个页面,只会在浏览器中显示&#34; http://localhost/proyect/validate.php&#34;和文本&#34;仪表板&#34;在页面上,但是,如果我在浏览器中写下&#34; http://localhost/proyect/dashboard.php&#34;加载包含所有信息的页面。

我不知道我做错了什么。 有人可以帮助我,我会非常感激,我已经好几天了。 感谢。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

请勿打印,请改为尝试:

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

感谢Magnus Eriksson的建议