php - 为什么我的 php 文件无法识别会话?
问题描述
我有两个文件:一个登录文件和一个视图文件。在 login.php 文件中,我开始一个这样的会话:“$_SESSION["who"] = $_POST["who"];” 当我按下登录按钮时,它会将我重定向到 view.php 文件。view.php 检查会话以查看是否设置了用户名,如果用户名不存在,view.php 必须使用 PHP die() 函数立即停止。
我的问题是,无论我是否输入用户名,总是使用 die() 函数。
这是我每个文件的代码。login.php 文件:
session_start();
if ( isset($_POST['cancel'] ) ) {
// Redirect the browser to game.php
header("Location: index.php");
return;
}
$salt = 'XyZzy12*_';
$stored_hash = '1a52e17fa899cf40fb04cfc42e6352f1'; // Pw is php123
$failure = false; // If we have no POST data
// Check to see if we have some POST data, if we do process it
if ( isset($_POST['who']) && isset($_POST['pass']) ) {
unset($_SESSION["who"]);
if ( strlen($_POST['who']) < 1 || strlen($_POST['pass']) < 1 ) {
$_SESSION["error"] = "User name and password are required";
header( 'Location: login.php' ) ;
return;
} else {
if (strpos($_POST['who'], '@') == false) {
$_SESSION["error"] = "Email must have an at-sign @";
header( 'Location: login.php' ) ;
return;
} else {
$check = hash('md5', $salt.$_POST['pass']);
if ( $check == $stored_hash ) {
$_SESSION["who"] = $_POST["who"];
header( 'Location: view.php' ) ;
return;
} else {
$_SESSION["error"] = "Incorrect password";
header( 'Location: login.php' ) ;
return;
}
}
}
}
// Fall through into the View
?>
<!DOCTYPE html>
<html>
<head>
<?php require_once "bootstrap.php"; ?>
<title>123</title>
</head>
<body>
<div class="container">
<h1>Please Log In</h1>
<?php
if ( isset($_SESSION["error"]) ) {
echo('<p style="color:red">'.htmlentities($_SESSION['error'])."</p>\n");
unset($_SESSION["error"]);
}
?>
<form method="POST">
<label for="who">Email</label>
<input type="text" name="who" id="who"><br/>
<label for="id_123">Password</label>
<input type="text" name="pass" id="id_1723"><br/>
<input type="submit" value="Log In">
<input type="submit" name="cancel" value="Cancel">
</form>
<p>
For a password hint, view source and find a password hint
in the HTML comments.
<!-- Hint: The password is php (all lower case) followed by 123. -->
</p>
</div>
</body>
view.php 文件:
<?php
if ( ! isset($_SESSION['who']) ) {
die('Not logged in');
}
require_once "pdo.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>123</title>
<?php require_once "bootstrap.php"; ?>
</head>
<body>
<div class="container">
<h1>Tracking Autos for <?php ?> </h1>
<h2>Automobiles</h2>
<p><a href="add.php">Add New</a> | <a href="logout.php">Logout</a></p>
</div>
</body>
</html>
解决方案
你忘了把 session_start(); 在 view.php 的开头。
<?php
session_start();
if ( ! isset($_SESSION['who']) ) {
die('Not logged in');
}
require_once "pdo.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>123</title>
<?php require_once "bootstrap.php"; ?>
</head>
<body>
<div class="container">
<h1>Tracking Autos for <?php ?> </h1>
<h2>Automobiles</h2>
<p><a href="add.php">Add New</a> | <a href="logout.php">Logout</a></p>
</div>
</body>
</html>
推荐阅读
- c++ - 如果在事件仍在执行时再次设置事件,RegisterWaitForSingleObject 是否会并行运行回调?
- twig - Symfony 4自己的表单主题未加载
- java - Eclipse gradle mc 修改错误
- excel - 跨多个类别的动态百分比分析 - PowerPivot / DAX
- android - 项目文件随机加密/损坏?
- java - 在方法中使用 Scanner 类
- python - 如何修复由 Python 中的函数引起的这个无限循环?
- android - 如何根据用户输入从可变资源字符串名称填充微调器?
- java - 覆盖超类中的私有方法-有办法吗?
- java - Selenium/Java - 将数据写入 Excel