Never store plain-text passwords. Use password_hash() to hash them with bcrypt or Argon2id, and password_verify() to check them on login. Never use MD5 or SHA1 for passwords — they are too fast and easily cracked.
Secure Password Handling
// NEVER store plain text passwords!
// NEVER use MD5 or SHA1!
// Hash a password (uses bcrypt by default)
$hash = password_hash("UserSecret123!", PASSWORD_DEFAULT);
// $2y$10$... (60 char bcrypt hash)
// Verify password
if (password_verify("UserSecret123!", $hash)) {
echo "Password correct!";
}
// Check if rehash needed (after upgrading algorithm)
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
$hash = password_hash("UserSecret123!", PASSWORD_DEFAULT);
// save new hash to database
}
// Argon2id (stronger — PHP 7.3+)
$hash = password_hash("secret", PASSWORD_ARGON2ID, [
"memory_cost" => 65536,
"time_cost" => 4,
"threads" => 3,
]);