Advertisement
Guest User

Untitled

a guest
May 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1.     //Login Class
  2.     class login extends account {
  3.         private $username;
  4.         private $password;
  5.         private $remember;
  6.         private $sess_id;
  7.        
  8.         //Account Construction Function
  9.         public function __construct($username,$password,$remember) {
  10.             $this->username = parent::clear_string($username);
  11.             $this->password = parent::clear_string($password);
  12.             $this->remember = $remember;
  13.            
  14.             $error_check = $this->error_check();
  15.            
  16.             if(!$error_check) {
  17.                 $this->sess_id = $this->set_sessID();
  18.                
  19.                 $session = $this->create_session();
  20.                 $cookie = $this->set_cookie();
  21.                
  22.                 if(!$cookie || !$session) {
  23.                     return false;
  24.                 } else {
  25.                     return true;
  26.                 }
  27.             }
  28.         }
  29.        
  30.         //Login form error check
  31.         private function error_check() {
  32.             //Status checker (of whether a user is logged in) will be elaborated on
  33.             //This is a temporary spot - it will be changed in the future
  34.             if(parent::check_status()) {
  35.                 throw new Exception('You are already logged in.');
  36.                 return true;
  37.             }
  38.            
  39.            
  40.             if(empty($this->username) || empty($this->password)) {
  41.                 throw new Exception('One or more fields were left blank.');
  42.                 return true;
  43.             }
  44.             if(!ctype_alnum($this->username) || !ctype_alnum($this->password)) {
  45.                 throw new Exception('Only alphanumeric characters may be used.');
  46.                 return true;
  47.             }
  48.             if(!parent::find_user($this->username)) {
  49.                 throw new Exception('User \''.$this->username.'\' does not exist in the database.');
  50.                 return true;
  51.             }
  52.             if(!$this->check_pswd()) {
  53.                 throw new Exception('Password entered for user \''.$this->username.'\' was incorrect.');
  54.                 return true;
  55.             }
  56.             if(!parent::check_validity($this->username)) {
  57.                 throw new Exception('User \''.$this->username.'\' has not been validated. Please check your email for a validation link, or contact an administrator if you did not recieve an email.');
  58.                 return true;
  59.             }
  60.             return false;
  61.         }
  62.        
  63.         //Check if the password matches the stored password
  64.         private function check_pswd() {
  65.             $sql_pswd = parent::get_info($this->username,'password');
  66.            
  67.             if(md5($this->password) != $sql_pswd) {
  68.                 return false;
  69.             } else {
  70.                 return true;
  71.             }
  72.         }
  73.        
  74.         //Create the session ID
  75.         private function set_sessID() {
  76.             $session = parent::str_rand(20);
  77.             $sess_id = md5($this->username.$this->password.$session);
  78.             return $sess_id;
  79.         }
  80.        
  81.         //Set the login cookie
  82.         private function set_cookie() {
  83.             switch ($this->remember) {
  84.                 case true:
  85.                     $cookie_expire = time()+60+60+24+30;
  86.                 break;
  87.                 case false:
  88.                     $cookie_expire = 0;
  89.                 break;
  90.             }
  91.            
  92.             $cookie = setcookie('knoxius_account',$this->sess_id,$cookie_expire,'/');
  93.            
  94.             if(!$cookie) {
  95.                 throw new Exception('You could not be logged in because you do not have cookies turned on.');
  96.                 return false;
  97.             } else {
  98.                 return true;
  99.             }
  100.         }
  101.        
  102.         //Set the login session
  103.         private function create_session() {
  104.             $_SESSION['knoxius_account'] = $this->sess_id;
  105.            
  106.             $query = 'INSERT INTO sessions VALUES(NULL,\''.$this->username.'\',\''.$this->sess_id.'\')';
  107.             $create_sess = mysql_query($query);
  108.            
  109.             if(!$create_sess) {
  110.                 throw new Exception('An unknown error occurred and you were not logged in.');
  111.                 return false;
  112.             } else {
  113.                 return true;
  114.             }
  115.         }
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement