mrkirby153

Untitled

Jan 16th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. class Login {
  4.  
  5.     private $_username;
  6.     private $_password;
  7.     public $_isCookie;
  8.  
  9.     private $_access;
  10.     public $_status;
  11.  
  12.     public function __construct() {
  13.         $this -> _access = 0;
  14.         if (isset($_SESSION['username'])) {
  15.             $this -> _username = $_SESSION['username'];
  16.         }
  17.         if (isset($_SESSION['password'])) {
  18.             $this -> _password = $_SESSION['password'];
  19.         }
  20.         if (isset($_COOKIE['cs_username']) && isset($_COOKIE['cs_login'])) {
  21.             $this -> _isCookie = 1;
  22.         }
  23.     }
  24.  
  25.     public function isLoggedIn() {
  26.         $this -> verifySession();
  27.         return $this -> _access;
  28.     }
  29.  
  30.     public function verifySession() {
  31.         if ($this -> sessionExist() && $this -> verifyDatabase())
  32.             $this -> _access = 1;
  33.     }
  34.  
  35.     public function sessionExist() {
  36.         return (isset($_SESSION['username']) && isset($_SESSION['password'])) ? 1 : 0;
  37.     }
  38.  
  39.     public function verifyDatabase() {
  40.         require_once (dirname(__FILE__) . "/../config.php");
  41.         global $config;
  42.         $DB_NAME = $config['db']['dbName'];
  43.         $DB_HOST = $config['db']['host'];
  44.         $DB_USER = $config['db']['username'];
  45.         $DB_PASS = $config['db']['password'];
  46.         $DB_PORT = $config['db']['port'];
  47.  
  48.         $db = new MySQLi($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_PORT);
  49.         if (mysqli_connect_errno()) {
  50.             printf("Connection failed: %s\n", mysqli_connect_error());
  51.             return false;
  52.         }
  53.         $sql = "SELECT * from cs_users WHERE username = '{$this->_username}'";
  54.         if ($this -> _isCookie) {
  55.             $sql = "SELECT * FROM cs_users WHERE username = '{$_COOKIE['cs_username']}'";
  56.         }
  57.         $data = $db -> query($sql);
  58.         if ($data -> num_rows) {
  59.             $row = $data -> fetch_assoc();
  60.             if ($this -> _isCookie) {
  61.                 $cookie = $_COOKIE['cs_login'];
  62.                 $parts = explode("-", $cookie);
  63.                 $userId = $parts[0];
  64.                 $token = $parts[1];
  65.                 $dbToken = md5($row['id'] . $row['username'] . $row['token']);
  66.                 if ($token == $dbToken) {
  67.                     return true;
  68.                 } else {
  69.                     return false;
  70.                 }
  71.             }
  72.             $hash = $row['password'];
  73.             return password_verify($this -> _password, $hash);
  74.         } else {
  75.             return false;
  76.         }
  77.         return false;
  78.     }
  79.  
  80.     public function getUsername() {
  81.         if (isset($_SESSION['username']))
  82.             return $_SESSION['username'];
  83.     }
  84.  
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment