Passwords and Security
Hash passwords with password_hash and check them with password_verify. Use prepared statements, escape output, CSRF tokens, HTTPS and session_regenerate_id after login.
Example
$hash = password_hash("secret123", PASSWORD_DEFAULT);
var_dump(password_verify("secret123", $hash));
var_dump(password_verify("wrong", $hash));
echo strlen(bin2hex(random_bytes(16))), "\n";
echo htmlspecialchars("<script>alert(1)</script>"), "\n";Result
bool(true) bool(false) 32 <script>alert(1)</script>
Exercise
Check a password.
($pw, $hash)Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.