Advertisement
Guest User

Login for Bryle Improved

a guest
May 25th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. class login {
  5.     private static $_instance = null;
  6.  
  7.     protected $MySQLi = null;
  8.  
  9.     public $Login;
  10.  
  11.     private function login($LoginData) {
  12.         extract($LoginData);
  13.  
  14.         $this->Login = new stdClass();
  15.         $this->Login->RESPONSE = false;
  16.         $this->Login->MESSAGE = '';
  17.  
  18.  
  19.         $this->initConnection($MySQL);
  20.         $UserData = $this->_sanetizeArray($UserData);
  21.         $this->initLogin($UserData);
  22.     }
  23.  
  24.     public function getInstance($LoginData) {
  25.         if(is_null(login::$_instance)) {
  26.             login::$_instance = new login($LoginData);
  27.         }
  28.  
  29.         return login::$_instance;
  30.     }
  31.  
  32.     protected function initLogin($UserData) {
  33.         extract($UserData);
  34.  
  35.         $inputCheck = $this->_inputCheck($Username, $Password);
  36.         if($inputCheck == false) {
  37.             $userCheck = $this->_userCheck($Username, $Password);
  38.             if($userCheck == true) {
  39.                 if($Remember != false) {
  40.                     setcookie('name', $Username, time() + 3600); // 3600 = 1hr
  41.                 } else {
  42.                     $_SESSION['name'] = $Username;
  43.                 }
  44.  
  45.                 $this->Login->MESSAGE = 'You have successfully been logged in.';
  46.                 $this->Login->RESPONSE = true;
  47.             } else {
  48.                 $this->Login->MESSAGE = 'The username or password is incorrect.';
  49.             }
  50.         } else {
  51.             $this->Login->MESSAGE = 'Do not leave any of the fields empty.';
  52.         }
  53.     }
  54.  
  55.     protected function initConnection($MySQL) {
  56.         extract($MySQL);
  57.  
  58.         $this->MySQLi = new mysqli($Hostname, $Username, $Password, $Database);
  59.         if($this->MySQLi != true) {
  60.             exit('Was unable to establish a connection: ' . $this->MySQLi->connect_error);
  61.         }
  62.     }
  63.  
  64.     private function _userCheck($Username, $Password) {
  65.         $query = $this->MySQLi->query("SELECT * FROM meh_users WHERE Username = '{$Username}' AND Password = '{$Password}' LIMIT 1");
  66.         return ($query->num_rows > 0) ? true : false;
  67.     }
  68.  
  69.     private function _inputCheck($Username, $Password) {
  70.         if(strlen($Username) == 0 || strlen($Password) == 0) {
  71.             return false;
  72.         }
  73.  
  74.         return true;
  75.     }
  76.  
  77.     private function _sanetizeArray($variable) {
  78.         $variable = filter_input_array(INPUT_POST, $variable);
  79.         return $variable;
  80.     }
  81. }
  82.  
  83. if(count($_POST) > 0) {
  84.     $login = login::getInstance(array(
  85.         'MySQL' => array(
  86.             'Hostname' => '127.0.0.1',
  87.             'Username' => 'root',
  88.             'Password' => 'faq',
  89.             'Database' => 'mrealms'
  90.         ),
  91.         'UserData' => array(
  92.             'Username' => $_POST['name'],
  93.             'Password' => $_POST['pwd'],
  94.             'Remember' => (isset($_POST['remember']) ? $_POST['remember'] : false)
  95.         )
  96.     ));
  97.  
  98.     echo  $login->Login->MESSAGE;
  99.     if($login->Login->RESPONSE != false) {
  100.         header('Refresh: 2.5;Url=index.php');
  101.     }
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement