Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. class Login
  3. {
  4.     private $_id;
  5.     private $_username;
  6.     private $_password;
  7.     private $_passmd5;
  8.    
  9.     private $_errors;
  10.     private $_access
  11.     private $_login
  12.     private $_token
  13.    
  14.     public function _construct()
  15.     {
  16.         $this->_errors = array();       //empty array to carry error messages
  17.         $this->_login = isset($_POST['login'])? 1 : 0; //login indicater, if login button is clicked = true else false
  18.         $this->_acess = 0;      //access area, level of access 0 by default
  19.         $this->_token = $_POST['token'];    //assigned form token
  20.        
  21.         $this->_id = 0; //id, default is 0
  22.         $this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION['username']; // has users submitted form? filter out post username variable and assigne to username
  23.         $this->_password = ($this->_login)? $this->filter($_POST['password']) : ''; // contains original password text, needed for validation
  24.         $this->_passmd5 = ($this->_login)? md5($this->_password) : $_SESSION['password']; //contains password in encrypted password
  25.        
  26.         //all rely on form being submitted
  27.        
  28.            
  29.     }
  30.    
  31.     public function isLoggedIn()
  32.     {
  33.         ($this->_login)? $this->verifyPost() : $this->verifySession(); //verify's post data and session
  34.        
  35.         return $this->_access; //checks access level
  36.     }
  37.    
  38.     public function filter()
  39.     {
  40.         return preg_replace('/[a-zA-Z0-9]/','',$var); //filters bad chars
  41.     }
  42.    
  43.     public function verifyPost()
  44.     {
  45.         try
  46.         {
  47.             if(!$this->isTokenValid())
  48.                 throw new Exeption('Invalid Form Submission');
  49.                
  50.             if(!$this->isDataValid())
  51.                 throw new Exception('Invalid Form Data');
  52.                
  53.             if(!$this->verifyDatabase());
  54.                 throw new Exception('Invalid Username or Password');
  55.                
  56.             $this->_acess = 1;
  57.             $this->registerSession();          
  58.         }
  59.     }
  60.     catch (Exception $e)
  61.     {
  62.         $this->_erorrs[] = $e->getMessage();
  63.     }
  64.    
  65.     public funtcion verifySession()
  66.     {
  67.         if($this->sessionExist() && $this->verifyDatabase();   
  68.     }
  69.    
  70.     public function verifyDatabase()
  71.     {
  72.         //Database Connection Info
  73.         mysql_connect("db_location", "username", "password") or die("Could not connect to database");
  74.         mysql_select_db("db_name") or die("Could not select databse");
  75.        
  76.         $data = mysql_query("SELECT IF FROM tablename WHERE username = '($this->_username)' AND password = '($this->_passmd5'");
  77.        
  78.         if(mysql_num_rows($data))
  79.             {
  80.                 list($this->_id) = @array_values(mysql_fetch_assoc($data));
  81.                 return true;
  82.             }
  83.         else
  84.             {
  85.                 return false;
  86.             }
  87.     }
  88.    
  89.     public function isDataValid()
  90.     {
  91.         return(preg_match('/^[a-zA-Z0-9)(5,12)$/',$this->_username) && (preg_match('/^[a-zA-Z0-9)(5,12)$/',$this->_password))? 1 : 0;
  92.     }
  93.    
  94.     public function isTokenValid()
  95.     {
  96.         return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
  97.     }
  98.    
  99.     public function registerSession()
  100.     {
  101.         $_SESSION['ID'] = $this->_id;
  102.         $_SESSION['username'] = $this->_username;
  103.         $_SESSION['password'] = $this->_passmd5;
  104.     }
  105.    
  106.     public function sessionExist()
  107.     {
  108.         return(isset($_SESSION['username']) && isset(['password']))? 1 : 0;
  109.    
  110.     public function showErrors()
  111.     {
  112.         echo "<h3>Errors></h3>";
  113.        
  114.         foreach($this->_errors as $key=>$value)
  115.             echo $value."<br>";
  116.     }
  117. }
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement