Secure login with sessions
Verify the password hash, then regenerate the session id so old ids cannot be reused.
PHP · Secure login with sessionsIntermediate
session_start();
$st = $pdo->prepare('SELECT id, password_hash FROM users WHERE username = ?');
$st->execute([$_POST['username'] ?? '']);
$user = $st->fetch();
if ($user && password_verify($_POST['password'] ?? '', $user['password_hash'])) {
session_regenerate_id(true);
$_SESSION['uid'] = $user['id'];
header('Location: dashboard.php');
exit;
}
$error = 'Wrong username or password.';loginsessionsecurity