Im trying to set a limited number of unlock/login attempts for my web app. Here is my code:
<?PHP
session_start();
if (isset($_SESSION["locked"])) {
$difference = time() - $_SESSION["locked"];
if ($difference > 10) {
unset($_SESSION["locked"]);
unset($_SESSION["login_attempts"]);
}
}
if (isset($_POST["user"]) && !isset($_SESSION["user"])) {
$users = ["admin" => "123456",];
if (isset($users[$_POST["user"]])) {
if ($users[$_POST["user"]] == $_POST["password"]) {
$_SESSION["user"] = $_POST["user"];
}
}
if (!isset($_SESSION["user"])) {
if (empty ($_POST["user"]) || empty ($_POST['password'])) {
$userErr= '<div role="alert">
MISSING INPUT! Please make sure to input both username and password.</div>';
$failed = true;
} else {
$_SESSION["login_attempts"] = 1;
$_SESSION["error"] = "It doesn't match!";
$userErr= '<div role="alert">
INVALID CREDENTIALS! Entered username and password doesnt match any user accounts.</div>';
$failed = true;
}
}
}
if (isset($_SESSION["user"])) {
header("Location: index.php");
exit();
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Unlock Message</title>
<style>
body{
background-color: #85FFBD;
background-image: linear-gradient(45deg, #85FFBD 0%,
#FFFB7D 100%);
background-size: cover;
display: grid;
align-items: center;
justify-content: center;
height: 100vh;
}
.container{
width: 35rem;
height: 25rem;
box-shadow: 0 0 1rem 0 rgba(0, 0, 0, .2);
border-radius: 20px;
background-color: rgba(255, 255, 255, .15);
backdrop-filter: blur(20px);
}
</style>
</head>
<body>
<?PHP
if (isset($failed)) {
echo $userErr;
}
?>
<div >
<h3 >Unlock Quote of the Day!</h3>
<p>In order to view the quote, please enter the user credentials.</p>
<div >
<?php
if(isset($_SESSION["error"])) {
?>
<p style="color: red;"><?=$_SESSION["error"]; ?></p>
<?php
unset($_SESSION["error"]); }
?>
<form id="login-form" method="post" target="_self">
<div >
<label for="user" >Username</label>
<input type="text" name="user" value="admin">
</div>
<div >
<label for="password" >Password</label>
<input type="password" name="password" value="123456">
</div>
<!-- <button type="submit" value="Sign In" >Unlock</button> -->
<?php
**if ($_SESSION["login_attempts"]> 2)**
{
$_SESSION["locked"] = time();
echo "Please wait for 10 seconds";
}
else
{
?>
<button type="submit" value="login_attempts">Unlock</button>
<?php
}
?>
</form>
</div>
However, I am returning the error:
Warning: Undefined array key "login_attempts" in C:\xampp\htdocs\login.php on line 114 (I turned this line bold to see)
How can I fix this? I feel like I should define login_attempts but how? Thank you!
CodePudding user response:
Try this next line to $users = [...]
if (! isset ($_SESSION["login_attempts"])) {
$_SESSION["login_attempts"] = 0;
}
