我正在尝试为我的大学网站启动会话,但代码既不工作也不显示任何错误。
这是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,但仍然以某种方式设置了会话。我做错了什么?
答案 0 :(得分:0)
考虑以下
ob_start()
- 但没关系connection.php
session_start()
醇>
希望这些输入有所帮助..