用户名和密码不正确时页面空白 - 登录 - PHP

时间:2017-01-17 18:35:49

标签: php html cookies login

我正在尝试使用PHP(没有MySQL或SQLite)创建登录系统。但由于某种原因,应该显示错误的代码,而细节不正确显示一个空白页面。如果他们是正确的工作。 (这只是测试的原型。我不会为每个用户制作一个" if / else") 我的代码:

<?php

if (isset($_POST['user']) && isset($_POST['pass'])) {
 if ($_POST['user'] == "jakeajames" && $_POST['pass'] == "test123") {
  setcookie("loggedin", "yes", time() + 60 * 60 * 3, "/");
   header("Location: home.php");
 }
elseif ($_POST['user'] != "jakeajames" && $_POST['pass'] != "test123" && isset($_COOKIE['failed3']))
{
  switch ($_COOKIE['failed3']) {
  case "1":
    setcookie("failed3", "2", time() + 60, "/");

echo '<html>
<head><title>Login</title></head>
<body>
<center>
<p style="color: red;"> 2 attempts remaining</p>
<form method="post">
Usename: <input type="text" placeholder="username" name="user"></input><br>
Password: <input type="password" placeholder="password" name="pass"></input><br>
<input type="submit"></input>
</form>
</body>
</html>';

  break;
  case "2":
 setcookie("failed3", "3", time() + 60, "/");
echo '<html>
<head><title>Login </title></head>
<body>
<center>
<p style="color: red;"> 1 attempt remaining</p>
<form method="post">
Usename: <input type="text" placeholder="username" name="user"></input><br>
Password: <input type="password" placeholder="password" name="pass"></input><br>
<input type="submit"></input>
</form>
</body>
</html>';
break;
case "3":
  echo "You need to wait 60 seconds till you can login again";
  break;
}


}
elseif ($_POST['user'] != "jakeajames" && $_POST['pass'] != "test123" && isset($_COOKIE['failed3'])) {
  setcookie("failed3", "1", time() + 60, "/");
}

}



if (!isset($_POST['user']) && !isset($_POST['pass'])) {
 echo '<html>
<head><title>Login</title></head>
<body>
<center>

<form method="post">
Usename: <input type="text" placeholder="username" name="user"></input><br>
Password: <input type="password" placeholder="password" name="pass"> </input><br>
<input type="submit"></input>
</form>
</body>
</html>';
}
?>

1 个答案:

答案 0 :(得分:1)

正确的行号接近50,你再次检查相同的条件

elseif ($_POST['user'] != "jakeajames" && $_POST['pass'] != "test123" && isset($_COOKIE['failed3'])) {
                                                                         ^add !here
     setcookie("failed3", "1", time() + 60, "/");
}

并且正确的是

elseif ($_POST['user'] != "jakeajames" && $_POST['pass'] != "test123" && !isset($_COOKIE['failed3'])) {
    setcookie("failed3", "1", time() + 60, "/");
}

,输出

after corrected