Add 'login.php'

main
ezn 3 years ago
parent 36fd794e27
commit 8cc24b0642

@ -0,0 +1,67 @@
<?php
// remove next 3 lines when you're done, so that errors don't show up in a browser
ini_set("display_errors", "1");
ini_set("display_startup_errors", "1");
error_reporting(E_ALL);
$is_invalid = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$mysqli = require __DIR__ . "/database.php";
$sql = sprintf("SELECT * FROM user WHERE email = '%s'", $mysqli->real_escape_string($_POST["email"]));
$result = $mysqli->query($sql);
$user = $result->fetch_assoc();
if ($user) {
if (password_verify($_POST["password"], $user["password_hash"])) {
session_start();
session_regenerate_id();
$_SESSION["user_id"] = $user["id"];
header("Location: index.php");
exit;
}
}
$is_invalid = true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
<h1>Login</h1>
<?php if ($is_invalid): ?>
<em>Invalid login</em>
<?php endif; ?>
<form method="post">
<label for="email">email</label>
<input type="email" name="email" id="email"
value="<?= htmlspecialchars($_POST["email"] ?? "") ?>">
<label for="password">password</label>
<input type="password" name="password" id="password">
<button>Log in</button>
</form>
</body>
</html>
Loading…
Cancel
Save