What if your users' passwords were stolen tomorrow? Learn how to keep them safe today!
Why Password hashing with password_hash in PHP? - Purpose & Use Cases
Imagine you have a website where users create accounts with passwords. You decide to store these passwords exactly as they are, in plain text, in your database.
One day, someone hacks your database and steals all the passwords. Now, every user's account is at risk because their passwords are exposed.
Storing passwords as plain text is very risky and careless. If the database leaks, all passwords are immediately visible to attackers.
Trying to create your own way to hide passwords by simple tricks or basic encryption is complicated and often insecure. It's easy to make mistakes that let hackers reverse your work.
Using password_hash in PHP automatically creates a strong, secure version of the password that can't be easily reversed.
This function handles the complex work of adding random data (called salt) and choosing the best hashing method, so you don't have to worry about it.
$hashed = md5($password);
$hashed = password_hash($password, PASSWORD_DEFAULT);
It lets you safely store passwords so even if your database is stolen, the real passwords stay secret and protected.
When a user logs into a bank website, their password is checked against a hashed version stored securely using password_hash. This keeps their money safe from hackers.
Storing plain passwords is dangerous and risky.
password_hash creates strong, secure password hashes automatically.
This protects user accounts even if your database is compromised.
Practice
password_hash function in PHP?Solution
Step 1: Understand what
This function takes a plain password and creates a secure hashed version that cannot be reversed.password_hashdoesStep 2: Compare options with function purpose
To securely convert a plain password into a hashed string correctly describes hashing. To verify if a password matches a hash is forpassword_verify. Options A and D are incorrect because passwords should never be stored in plain text or decrypted.Final Answer:
To securely convert a plain password into a hashed string -> Option DQuick Check:
password_hash = create secure hash [OK]
- Confusing hashing with verifying passwords
- Thinking passwords can be decrypted
- Storing passwords as plain text
password_hash with the default algorithm?Solution
Step 1: Recall the correct function signature
Thepassword_hashfunction requires the password string and the algorithm constant, e.g.,PASSWORD_DEFAULT.Step 2: Check each option's syntax
$hash = password_hash($password, PASSWORD_DEFAULT); correctly passes the password variable and the constant. $hash = password_hash($password, 'default'); uses a string instead of constant. $hash = password_hash($password, default); uses undefined constant "default". $hash = password_hash(PASSWORD_DEFAULT, $password); swaps arguments incorrectly.Final Answer:
$hash = password_hash($password, PASSWORD_DEFAULT); -> Option AQuick Check:
password_hash(password, algorithm) correct syntax [OK]
- Using string 'default' instead of PASSWORD_DEFAULT
- Omitting the algorithm parameter
- Swapping argument order
$password = 'mypassword';
$hash = password_hash($password, PASSWORD_DEFAULT);
echo password_verify('mypassword', $hash) ? 'Valid' : 'Invalid';Solution
Step 1: Understand password_hash and password_verify usage
The code hashes 'mypassword' and then verifies the same password against the hash.Step 2: Evaluate the verification result
Since the password matches the hash,password_verifyreturns true, so 'Valid' is printed.Final Answer:
Valid -> Option BQuick Check:
password_verify correct password = true [OK]
- Expecting hash to be plain text
- Confusing verify with hash
- Assuming password_verify returns the hash
$password = 'secret';
$hash = password_hash($password);
if (password_verify($hash, $password)) {
echo 'Access granted';
} else {
echo 'Access denied';
}Solution
Step 1: Check password_verify function call
Thepassword_verifyfunction takes the plain password as first argument and the hash as second. In the code, the arguments are reversed: hash first, then password.Step 2: Verify other parts of the code
Thepassword_hashcall is correct (uses PASSWORD_DEFAULT by default), $password is defined, echo statements are fine.Final Answer:
password_verify arguments are reversed -> Option CQuick Check:
password_verify(plain_password, hash) [OK]
- Forgetting the algorithm argument
- Swapping arguments in password_verify
- Assuming password_hash works with one argument
password_hash and password_verify?Solution
Step 1: Check correct usage of password_hash and password_verify
$hash = password_hash($userPassword, PASSWORD_DEFAULT); // Store $hash in database // Later during login if (password_verify($inputPassword, $hash)) { echo 'Login successful'; } else { echo 'Login failed'; }hashes the password withPASSWORD_DEFAULTand stores the hash. Later, it verifies the input password against the stored hash correctly.Step 2: Identify errors in other options
$hash = password_hash($userPassword); // Store $hash if (password_verify($hash, $inputPassword)) { echo 'Login successful'; } else { echo 'Login failed'; }misses the algorithm and reverses arguments inpassword_verify.$hash = md5($userPassword); // Store $hash if (password_verify($inputPassword, $hash)) { echo 'Login successful'; } else { echo 'Login failed'; }uses insecuremd5instead ofpassword_hash.$hash = password_hash($userPassword, PASSWORD_DEFAULT); // Store $userPassword if (password_verify($inputPassword, $userPassword)) { echo 'Login successful'; } else { echo 'Login failed'; }stores plain password and verifies incorrectly.Final Answer:
Option A correctly hashes and verifies passwords securely -> Option AQuick Check:
Hash with password_hash, verify with password_verify [OK]
- Using md5 instead of password_hash
- Swapping arguments in password_verify
- Storing plain passwords instead of hashes
Ready to prove it?
Pass the test and this pattern moves unknown
