Tsallaka zuwa abun ciki
Justice Tech School

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
&lt;script&gt;alert(1)&lt;/script&gt;

Exercise

Check a password.

($pw, $hash)

Quiz Test

Which hashes passwords?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.