Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Login {
- private $_username;
- private $_password;
- public $_isCookie;
- private $_access;
- public $_status;
- public function __construct() {
- $this -> _access = 0;
- if (isset($_SESSION['username'])) {
- $this -> _username = $_SESSION['username'];
- }
- if (isset($_SESSION['password'])) {
- $this -> _password = $_SESSION['password'];
- }
- if (isset($_COOKIE['cs_username']) && isset($_COOKIE['cs_login'])) {
- $this -> _isCookie = 1;
- }
- }
- public function isLoggedIn() {
- $this -> verifySession();
- return $this -> _access;
- }
- public function verifySession() {
- if ($this -> sessionExist() && $this -> verifyDatabase())
- $this -> _access = 1;
- }
- public function sessionExist() {
- return (isset($_SESSION['username']) && isset($_SESSION['password'])) ? 1 : 0;
- }
- public function verifyDatabase() {
- require_once (dirname(__FILE__) . "/../config.php");
- global $config;
- $DB_NAME = $config['db']['dbName'];
- $DB_HOST = $config['db']['host'];
- $DB_USER = $config['db']['username'];
- $DB_PASS = $config['db']['password'];
- $DB_PORT = $config['db']['port'];
- $db = new MySQLi($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_PORT);
- if (mysqli_connect_errno()) {
- printf("Connection failed: %s\n", mysqli_connect_error());
- return false;
- }
- $sql = "SELECT * from cs_users WHERE username = '{$this->_username}'";
- if ($this -> _isCookie) {
- $sql = "SELECT * FROM cs_users WHERE username = '{$_COOKIE['cs_username']}'";
- }
- $data = $db -> query($sql);
- if ($data -> num_rows) {
- $row = $data -> fetch_assoc();
- if ($this -> _isCookie) {
- $cookie = $_COOKIE['cs_login'];
- $parts = explode("-", $cookie);
- $userId = $parts[0];
- $token = $parts[1];
- $dbToken = md5($row['id'] . $row['username'] . $row['token']);
- if ($token == $dbToken) {
- return true;
- } else {
- return false;
- }
- }
- $hash = $row['password'];
- return password_verify($this -> _password, $hash);
- } else {
- return false;
- }
- return false;
- }
- public function getUsername() {
- if (isset($_SESSION['username']))
- return $_SESSION['username'];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment