Advertisement
Guest User

Untitled

a guest
May 25th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. <?php
  2. //error_reporting(E_ALL); ini_set('display_errors', 1);
  3.  
  4. // input from user
  5. $input_username = "michael";
  6. $input_password = "password";
  7.  
  8. // user retrieved from db
  9. $user = retrieve_user($input_username);
  10.  
  11. // if user's exists, verify the password
  12. if ($user) {
  13.  
  14.     $db_hash = $user['password'];
  15.  
  16.     // combine username and password of the user
  17.     $input = urlencode("$input_username:$input_password");
  18.  
  19.     // verify user's input
  20.     if (password_verify($input, $db_hash)) {
  21.         echo "password verified";
  22.     } else {
  23.         echo "password incorrect";
  24.     }
  25. } else {
  26.     echo "username or password invalid";
  27. }
  28.  
  29. /**
  30.  * Retrieve user from database
  31.  */
  32. function retrieve_user($username)
  33. {
  34.     // ... get user's record by his username
  35.     // SELECT id, username, password FROM user WHERE username = $username
  36.  
  37.     // this is generated when user is created
  38.     $hash = password_hash(urlencode("michael:password"), PASSWORD_DEFAULT);
  39.  
  40.     // sample of user's record
  41.     $user = [
  42.         'id' => 1,
  43.         'username' => 'adam',
  44.         'password' => $hash,
  45.     ];
  46.  
  47.     return $user;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement