Store a password hash
<?php
$password = $_POST['password'] ?? '';
if (strlen($password) < 12) {
http_response_code(422);
exit('Choose a longer password.');
}
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare(
'INSERT INTO users (email, password_hash)
VALUES (:email, :password_hash)'
);
$stmt->execute([
'email' => $email,
'password_hash' => $hash,
]);
Verify during login
$stmt = $pdo->prepare(
'SELECT id, password_hash FROM users WHERE email = :email'
);
$stmt->execute(['email' => $email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password_hash'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = (int) $user['id'];
}
Security notes
Use a sufficiently sized column for hashes, such as VARCHAR(255). Add rate limiting, generic login errors, secure session-cookie settings, and a password-reset process. Never store plaintext passwords.