会话没有开始

时间:2017-04-28 11:31:16

标签: php session session-variables

我正在尝试为我的大学网站启动会话,但代码既不工作也不显示任何错误。

这是index.php文件:

 <?php
ob_start();
session_start();
require("connection.php");

if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id']) ) {


       header("Location: home.html");
  }else{

    header("Location: login.php");
    }

?>

这是login.php文件

<?php
require("connection.php");
    if($_POST)
    {
         $computer_code=$_POST["computer_code"];
         $password=$_POST["password"];

         //$password_hash = md5($_POST["password"]);

        if(!empty($password) && !empty($computer_code)){

        //$con=new mysqli("localhost","root","","bahubali_ips");
     $query="select Computer_Code,Password from student_info where Computer_Code = '".$computer_code."' AND Password = '".$password."'";


     $result=$con->query($query);

        if($row=$result->fetch_assoc()){


            if($row["Computer_Code"] == $computer_code && $row["Password"] == $password){
                $user_id = $row["Computer_Code"];
                $_SESSION['user_id'] = $user_id;
                header("Location: index.php");

            }

         }else{
            echo "Invalid computer_code / Password combination";

            die();
         }


        }
    else{
        echo "all fields required";
    }   
    }

  ?><form action="login.php" method="POST">...........</form>

在这里,每当我使用正确的凭据登录而不是启动会话并将我发送到home.html时,条件总是出现错误,因此我会一次又一次地看到登录表单。

我已经回显了$_SESSION['user_id']变量,它确实包含了user_id,但仍然以某种方式设置了会话。我做错了什么?

1 个答案:

答案 0 :(得分:0)

考虑以下

  1. 大部分时间你都不需要ob_start() - 但没关系
  2. 不是在每个页面中启动会话,而是将其置于 connection.php
  3. 顶部
  4. 如果您开始会话超过1次,则可能会收到错误。
  5. 在您显示的代码中,我在 login.php
  6. 中看不到session_start()

    希望这些输入有所帮助..