Guest User

Untitled

a guest
Jun 10th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. controller
  2.     public function register()
  3.     {
  4.             $register = User::create(array(
  5.                 'username' => 'test1',
  6.                 'password' => 'testpass'
  7.             ));
  8.  
  9.             $register->re_password = 'testpass';
  10.  
  11.             if($register->is_valid())
  12.             {
  13.                 echo 'Login';
  14.             }
  15.             else
  16.             {
  17.                 print_r($register->errors->full_messages());
  18.             }
  19.        
  20.     }
  21.  
  22.  
  23. Model
  24. <?php
  25. class User extends ActiveRecord\Model
  26. {
  27.     var $password = FALSE;
  28.     var $re_password = FALSE;
  29.  
  30.     static $validates_presence_of = array(
  31.         array('username'),
  32.         array('password')
  33.     );
  34.  
  35.     static $validates_size_of = array(
  36.         array('username', 'within' => array(3,15)),
  37.         array('password', 'minimum' => 6)
  38.     );
  39.  
  40.     public function validate()
  41.     {
  42.         if($this->password != $this->re_password && $this->re_password != $this->password)
  43.         {
  44.  
  45.             echo $this->password;
  46.  
  47.  
  48.             $this->errors->add('Password', "and retype password must me be the same.");
  49.         }
  50.     }
  51.  
  52.     function before_save()
  53.     {
  54.         if($this->password)
  55.         {
  56.             $this->hashed_password = $this->hash_password($this->password);
  57.         }
  58.     }
  59.  
  60.     private function hash_password($password)
  61.     {
  62.         $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
  63.         $hash = hash('sha256', $salt . $password);
  64.  
  65.         return $salt . $hash;
  66.     }'
Add Comment
Please, Sign In to add comment