Advertisement
cafreak

functions.class.php #3 [Mike M. tutorial]

Aug 17th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. #region login only
  2.        
  3.         public function receive_password($username){
  4.             //get hashed password from the database
  5.             global $dbhost, $dbusername, $dbpassword, $dbname;
  6.             $con = new Database($dbhost, $dbusername, $dbpassword, $dbname);
  7.             $query = $con->runQuery("SELECT * FROM `users` WHERE `username`='".$username."'");
  8.             if($query->rowCount() != 0){
  9.                 while($row_info = $query->fetch(PDO::FETCH_ASSOC)){
  10.                     $userInfo = new stdClass();
  11.                     $userInfo->UID = $row_info['ID'];
  12.                     $userInfo->UName = $row_info['username'];
  13.                     $userInfo->UPass = $row_info['password'];
  14.                     $userInfo->USalt = $row_info['salt'];
  15.                     //userInfo is succesfully loaded!
  16.                     //close the connection
  17.                     $con = null;
  18.                     //return the password!
  19.                     return $userInfo->UPass;
  20.                 }
  21.             }else{
  22.                 //error spotted make sure to close just in case
  23.                 //close the connection
  24.                 $con = null;
  25.                 die("Error: No users found!");
  26.             }
  27.         }
  28.        
  29.         public function receive_salt($username){
  30.             //get the salt from the database
  31.             global $dbhost, $dbusername, $dbpassword, $dbname;
  32.             $con = new Database($dbhost, $dbusername, $dbpassword, $dbname);
  33.             $query = $con->runQuery("SELECT * FROM `users` WHERE `username`='".$username."'");
  34.             if($query->rowCount() != 0){
  35.                 while($row_info = $query->fetch(PDO::FETCH_ASSOC)){
  36.                     $userInfo = new stdClass();
  37.                     $userInfo->UID = $row_info['ID'];
  38.                     $userInfo->UName = $row_info['username'];
  39.                     $userInfo->UPass = $row_info['password'];
  40.                     $userInfo->USalt = $row_info['salt'];
  41.                     //userInfo is succesfully loaded!
  42.                     //close the connection
  43.                     $con = null;
  44.                     //return the password!
  45.                     return $userInfo->USalt;
  46.                    
  47.                 }
  48.             }else{
  49.                 //error spotted make sure to close just in case
  50.                 //close the connection
  51.                 $con = null;
  52.                 die("Error: No users found!");
  53.             }
  54.         }
  55.        
  56.         public function equal_password($username, $password){
  57.             //check if the passwords matches
  58.             $password = $this->create_hashed_password($password, $this->receive_salt($username)); //creates the hashed password from the given password using the DB salt
  59.             $hashedPassword = $this->receive_password($username); //receives the hashed password from the database through the "receive_passworrd function"
  60.            
  61.             if($password == $hashedPassword){
  62.                 return true;
  63.             }else{
  64.                 return false;
  65.             }
  66.            
  67.         }
  68.        
  69.     #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement