0
0
PHPprogramming~10 mins

Password hashing with password_hash in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to hash the password using password_hash.

PHP
<?php
$password = 'mypassword';
$hash = password_hash($password, [1]);
echo $hash;
?>
Drag options to blanks, or click blank then click option'
APASSWORD_BCRYPT
BPASSWORD_MD5
CPASSWORD_SHA256
DPASSWORD_PLAIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsupported constants like PASSWORD_MD5 or PASSWORD_SHA256.
Passing the password string instead of the algorithm constant.
2fill in blank
medium

Complete the code to verify the password against the hash.

PHP
<?php
$password = 'mypassword';
$hash = password_hash($password, PASSWORD_BCRYPT);
if (password_verify($password, [1])) {
    echo 'Password is valid';
} else {
    echo 'Invalid password';
}
?>
Drag options to blanks, or click blank then click option'
A$password
B$hash
C'mypassword'
DPASSWORD_BCRYPT
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the plain password instead of the hash as the second argument.
Passing the algorithm constant instead of the hash.
3fill in blank
hard

Fix the error in the code to correctly hash the password.

PHP
<?php
$password = 'secret';
$hash = password_hash([1], PASSWORD_BCRYPT);
echo $hash;
?>
Drag options to blanks, or click blank then click option'
Apassword
B'password'
C$hash
D$password
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string 'password' instead of the variable.
Passing an undefined variable or the hash variable itself.
4fill in blank
hard

Fill both blanks to create a dictionary of usernames and their hashed passwords, only for users with passwords longer than 6 characters.

PHP
<?php
$users = ['alice' => 'secret123', 'bob' => '123', 'carol' => 'mypassword'];
$hashedUsers = array_filter(array_map(function($password) {
    return password_hash($password, [1]);
}, $users), function($password, $username) use ($users) {
    return strlen($users[$username]) > 6;
});
print_r($hashedUsers);
?>
Drag options to blanks, or click blank then click option'
APASSWORD_BCRYPT
B'alice'
C'bob'
D'carol'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong constants for hashing.
Checking password length for a user with a short password.
5fill in blank
hard

Fill all three blanks to create a filtered array of usernames and hashed passwords where the password length is greater than 5 and the username starts with 'a'.

PHP
<?php
$users = ['alice' => 'secret123', 'bob' => '123456', 'anna' => 'mypassword'];
$filteredHashed = array_filter(array_map(function($user, $password) {
    return [$user => password_hash($password, [1])];
}, array_keys($users), $users), function($user, $password) {
    return strlen($password) [2] 5 && str_starts_with($user, [3]);
}, ARRAY_FILTER_USE_BOTH);
print_r($filteredHashed);
?>
Drag options to blanks, or click blank then click option'
APASSWORD_BCRYPT
B>
C'a'
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Not quoting the letter 'a' in the string check.
Using wrong hashing algorithm constants.