Advertisement
cafreak

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

Aug 17th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. class functions{
  2.    
  3.     #region main
  4.    
  5.         #region username related
  6.             public function check_username($username){
  7.                 $length = new stdClass();
  8.                 $length->min = 4;
  9.                 $length->max = 10;
  10.                 $length->username = strlen($username);
  11.                 if($length->username >= $length->min && $length->username <= $length->max){
  12.                     return ctype_alnum($username);
  13.                 }else{
  14.                     return false;
  15.                 }
  16.             }
  17.            
  18.             public function username_exists($username){
  19.             //get the username from the database
  20.             global $dbhost, $dbusername, $dbpassword, $dbname;
  21.             $con = new Database($dbhost, $dbusername, $dbpassword, $dbname);
  22.             $query = $con->runQuery("SELECT * FROM `users` WHERE `username`='".$username."'");
  23.             if($query->rowCount() != 0){
  24.                 while($row_info = $query->fetch(PDO::FETCH_ASSOC)){
  25.                     $userInfo = new stdClass();
  26.                     $userInfo->UID = $row_info['ID'];
  27.                     $userInfo->UName = $row_info['username'];
  28.                     $userInfo->UPass = $row_info['password'];
  29.                     $userInfo->USalt = $row_info['salt'];
  30.                     //userInfo is succesfully loaded!
  31.                     //close the connection
  32.                     $con = null;
  33.                     //return the password!
  34.                     return true;
  35.                 }
  36.             }else{
  37.                 //error spotted make sure to close just in case
  38.                 //close the connection
  39.                 $con = null;
  40.                 return false;
  41.             }
  42.         }
  43.         #end
  44.        
  45.         #region password related
  46.             public function check_password($password){
  47.                 $length = new stdClass();
  48.                 $length->min = 6;
  49.                 $length->max = 20;
  50.                 $regex = '/^[a-zA-Z0-9 \!\@\#\$\%\^\&\*]{'.$length->min.','.$length->max.'}$/';
  51.                 if(preg_match($regex, $password)){
  52.                     return true;
  53.                 }else{
  54.                     return false;
  55.                 }
  56.             }
  57.            
  58.             public function create_hashed_password($password, $salt){
  59.                 $blowFishSalt = '$2y$12$'.$salt.'$';
  60.                 return(crypt($password.$salt,$blowFishSalt));
  61.             }
  62.    
  63.     #end
  64.    
  65.     #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement