The following files exists in this folder. Click to view.
login.php73 lines ASCII Unix (LF) 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
<?php
session_start();
require_once "functions/database.php";
function attempt_login($username, $password)
{
global $db;
$result = $db->query("SELECT userID, `password` FROM accountData WHERE username = :username", array("username" => $username));
if (!$result)
return null;
$hash = hash("sha256", $password, false);
if ($result["password"] == $hash)
return $result["userID"];
return null;
}
$login_failed = false;
if (isset($_POST["username"]) && isset($_POST["password"])) {
$userID = attempt_login($_POST["username"], $_POST["password"]);
if ($userID)
$_SESSION["userID"] = $userID;
else
$login_failed = true;
}
if(isset($_SESSION["userID"])){
header("location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Log in</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
crossorigin="anonymous"></script>
</head>
<body>
<?php include "page/navbar.php" ?>
<div class="container">
<br>
<h1>Log in</h1><br>
<?php if ($login_failed)
echo '<p class="text-danger">Login failed!</p>'; ?>
<form action="login.php" method="POST">
<label for="username" class="form-label">Username</label>
<div class="input-group mb-3">
<input type="text" id="username" name="username" class="form-control" placeholder="Username"
aria-label="Username" aria-describedby="basic-addon1" required>
</div>
<br>
<label for="password" class="form-label">Password</label>
<div class="input-group mb-3">
<input type="password" id="password" name="password" class="form-control" placeholder="Password"
aria-label="Password" aria-describedby="basic-addon1" required>
</div>
<input type="submit" class="btn btn-primary" value="Log in">
<br>
<a href="signup.php">Sign up instead</a>
</form>
</div>
</body>
</html>