Advertisement
Stratician

Lets play Catch with PHP

Feb 5th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. class Signup {
  4.    
  5.     // Properties associated with the user registration form
  6.  
  7.     protected $username;
  8.     protected $email;
  9.     protected $password;
  10.     protected $missingFields = true;
  11.  
  12.     // Check to make sure the user entered information in all required form fields
  13.  
  14.     protected function validate($username, $password, $email)
  15.     {
  16.         try
  17.         {
  18.             if ($_POST && !empty($username) && !empty($password) && !empty($email))
  19.             {
  20.                 $this->missingFields = false;
  21.             }
  22.             else
  23.             {
  24.                 throw new Exception('Missing Required Fields');
  25.             }
  26.         }
  27.  
  28.         catch(Exception $e)
  29.         {
  30.             var_dump($e->getMessage());
  31.         }  
  32.     }
  33.  
  34.  
  35.     public function __construct($username, $password, $email)
  36.     {
  37.         $this->validate($username, $password, $email);
  38.  
  39.         try
  40.         {
  41.             if($this->missingFields === false)
  42.             {
  43.                 $this->username = $username;
  44.                 $this->email = $email;
  45.                 $this->hashPass($password);
  46.             }
  47.  
  48.             else
  49.             {
  50.                 throw new Exception('Could Not Validate User Fields');
  51.             }
  52.         }
  53.  
  54.         catch(Exception $e)
  55.         {
  56.             $e->getMessage();
  57.         }
  58.     }
  59.  
  60.  
  61.     // Create and store a hash from the user's submitted password
  62.  
  63.     protected function hashPass($hashedPass)
  64.     {
  65.         $this->password = password_hash($hashedPass, PASSWORD_DEFAULT);
  66.     }
  67.  
  68.  
  69.     // Dump the hashed password
  70.    
  71.     public function showHash()
  72.     {  
  73.         try
  74.         {
  75.             if($this->missingFields === false)
  76.             {
  77.                 var_dump($this->password);
  78.             }
  79.  
  80.             else
  81.             {
  82.                 throw new Exception('Password could not be hashed');
  83.             }
  84.         }
  85.        
  86.         catch(Exception $e)
  87.         {
  88.             var_dump($e->getMessage());
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement