当用户输入脚本的用户名和密码时,为什么不将其转发到受保护的php页面?有人可以帮我修复我的脚本吗?
我已经检查了文件名,它们是正确的,所以我不确定下一步该怎么做,我看不到问题所在。因此,我想在这里写一篇有关它的文章。
非常感谢您的帮助!
login.php脚本
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST["username"];
$pass = $_POST["password"];
$validated = false;
session_start();
if($user!=""&&$pass!="")
{
if($user=="test1"&&$pass=="test1")
$validated = true;
if($validated)
{
$_SESSION['login'] = "OK";
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
header('Location: protected.php');
}
else
{
$_SESSION['login'] = "";
echo "Invalid username or password.";
}
}
else $_SESSION['login'] = "";
}
?>
<html>
<body>
<h1>Login Page</h1>
<p>Please enter your username and password:</p>
<form action="login.php" method="post">
<table>
<tr>
<td align="right">Username: </td>
<td><input size=\"20\" type="text" size="20" maxlength="15" name="username"></td>
</tr>
<tr>
<td align="right">Password: </td>
<td><input size=\"20\" type="password" size="20" maxlength="15" name="password"></td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="left"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
受保护的脚本
<?php
session_start();
if($_SESSION['login'] != "OK")
{
header('Location: login.php');
exit();
}
?>
<html>
<head
<title>Protected Web Page</title>
</head>
<body>
<h1>Protected Web Page</h1>
<<p>You have successfully logged in!</p>
<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
<p>
Your username is: <?= $_SESSION['username'] ?=><br/>
Your password is: <?= $_SESSION['password'] ?>
</p>
</body>
</html>
答案 0 :(得分:0)
您有一些错误和毫无意义的代码(在注释中说明)-这将起作用: (在//后删除文字)
protected.php
<?php
session_start();
if($_SESSION['login'] != "OK"){
header('Location: login.php');
exit();
}
?>
<html>
<head> // Missing closing bracket >
<title>Protected Web Page</title>
</head>
<body>
<h1>Protected Web Page</h1>
<p>You have successfully logged in!</p> // had extra < at begining of line
<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
<p>
Your username is: <?php $_SESSION['username'] ?><br/> // dont use PHP opening and closing shortcodes, incorrect close
Your password is: <?php $_SESSION['password'] ?> // dont use PHP opening and closing shortcodes
</p>
</body>
</html>
login.php
<?php
session_start(); // start session at top
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST["username"];
$pass = $_POST["password"];
if($user != "" || $pass != ""){ // should use || for OR, not AND
if($user=="test1"&&$pass=="test1"){ // this wilkl only work with this combination
// removed validation boolean it's not needed
$_SESSION['login'] = "OK";
$_SESSION['username'] = $user;
$_SESSION['password'] = $pass;
header('Location: protected.php');
}else{
$_SESSION['login'] = "";
echo "Invalid username or password.";
}
}else{
$_SESSION['login'] = "";
echo "Username or password cannot be blank"; // added echo for blank inputs
}
}
?>
<html>
<body>
<h1>Login Page</h1>
<p>Please enter your username and password:</p>
<form method="post">
<table>
<tr>
<td align="right">Username: </td>
<td><input size="20" type="text" size="20" maxlength="15" name="username"></td>
</tr>
<tr>
<td align="right">Password: </td>
<td><input size="20" type="password" size="20" maxlength="15" name="password"></td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="left"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>