【发布时间】:2020-12-24 04:51:16
【问题描述】:
我有两个文件:一个登录文件和一个视图文件。 在 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()