So maybe we should add some more users. The easiest way to do this is to use an array.
<?php
session_start();
$users = [
['stefan','password'],
['admin','admin'],
['user','user']
];
$found = false;
if(isset($_POST['loginname']) && isset($_POST['password'])) {
$loginname = $_POST['loginname'];
$password = $_POST['password'];
foreach ($users as $user) {
if ($loginname == $user[0] && $password == $user[1]) {
$found = true;
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $loginname;
break;
}
}
if ($found) {
echo "Login successful! Welcome, " . htmlspecialchars($loginname) . "!";
} else {
echo "Please enter both username and password.";
}
}
if(!$found){
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Webtastic</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="index.php" method="post">
<label>Username: </label><input type="text" name="loginname" id="loginname" />
<br/>
<label>Password: </label><input type="password" name="password" id="password" />
<br/>
<button>Login please!</button>
</form>
</body>
</html>
<?php
}
I have also added a session.
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header("Location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Webtastic</h1>
<h2>Presentation</h2>
<p>Welcome <?php echo $_SESSION['username'];?> to the Webtastic presentation page!</p>
</body>
</html>