Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //error_reporting(E_ALL); ini_set('display_errors', 1);
- // input from user
- $input_username = "michael";
- $input_password = "password";
- // user retrieved from db
- $user = retrieve_user($input_username);
- // if user's exists, verify the password
- if ($user) {
- $db_hash = $user['password'];
- // combine username and password of the user
- $input = urlencode("$input_username:$input_password");
- // verify user's input
- if (password_verify($input, $db_hash)) {
- echo "password verified";
- } else {
- echo "password incorrect";
- }
- } else {
- echo "username or password invalid";
- }
- /**
- * Retrieve user from database
- */
- function retrieve_user($username)
- {
- // ... get user's record by his username
- // SELECT id, username, password FROM user WHERE username = $username
- // this is generated when user is created
- $hash = password_hash(urlencode("michael:password"), PASSWORD_DEFAULT);
- // sample of user's record
- $user = [
- 'id' => 1,
- 'username' => 'adam',
- 'password' => $hash,
- ];
- return $user;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement