Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2.  
  3. class Authentication {
  4.     private $dbHost = '127.0.0.1';
  5.     private $dbUser = 'root';
  6.     private $dbPassword = '';
  7.     private $dbName = 'crawl';
  8.     private $dbTable = 'usr';
  9.    
  10.     private $connection = null;
  11.    
  12.     public function __construct() {
  13.         if ($this->connection === null) {
  14.             try {
  15.                 $this->connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
  16.             } catch (Exception $e) {
  17.                 error_log($e->getMessage());
  18.             }
  19.         }
  20.         if (session_status() === PHP_SESSION_NONE) {
  21.             session_start();
  22.         }
  23.     }
  24.    
  25.     public function register($username, $password): bool {
  26.         if ($this->findByUsername($username) === null) {
  27.             if ($stmt = $this->connection->prepare("INSERT INTO " . $this->dbTable . " (username, password) VALUES (?, ?)")) {
  28.                 $hash = password_hash($password, PASSWORD_DEFAULT);
  29.                 $stmt->bind_param("ss", $username, $hash);
  30.                 $stmt->execute();
  31.                 return $stmt->affected_rows === 1 ? true : false;
  32.             }
  33.         }
  34.         return false;
  35.     }
  36.    
  37.     public function unregister($username): bool {
  38.         if ($this->findByUsername($username) !== null) {
  39.             if ($stmt = $this->connection->prepare("DELETE FROM " . $this->dbTable . " WHERE username = ?")) {
  40.                 $stmt->bind_param("s", $username);
  41.                 $stmt->execute();
  42.                 return $stmt->affected_rows === 1 ? true : false;
  43.             }
  44.         }
  45.         return false;
  46.     }
  47.    
  48.     public function login($username, $password): bool {
  49.         if ($stmt = $this->connection->prepare("SELECT password FROM " . $this->dbTable . " WHERE username = ? LIMIT 1")) {
  50.             $stmt->bind_param("s", $username);
  51.             $stmt->execute();
  52.             $stmt->bind_result($hash);
  53.             $stmt->store_result();
  54.             $stmt->fetch();
  55.  
  56.             if ($stmt->num_rows === 1) {
  57.                 if (password_verify($password, $hash)) {
  58.                     $_SESSION['username'] = $username;
  59.                 }
  60.             }
  61.         }
  62.         return false;
  63.     }
  64.    
  65.     public function logout(): void {
  66.         unset($_SESSION['username']);
  67.     }
  68.  
  69.     public function isAuthed(): bool {
  70.         if (array_key_exists('username', $_SESSION) && $_SESSION['username'] !== null) {
  71.             return true;
  72.         } else {
  73.             return false;
  74.         }
  75.     }
  76.    
  77.     public function getCurrentUser(): ?array {
  78.         if ($this->isAuthed()) {
  79.             return $this->findByUsername($_SESSION['username']);
  80.         }
  81.         return null;
  82.     }
  83.    
  84.     public function findByUsername($username): ?array {
  85.         if ($stmt = $this->connection->prepare("SELECT id, username FROM " . $this->dbTable . " WHERE username = ? LIMIT 1")) {
  86.             $stmt->bind_param("s", $username);
  87.             $stmt->execute();
  88.             $stmt->bind_result($id, $found);
  89.             $stmt->store_result();
  90.             $stmt->fetch();
  91.             if ($stmt->num_rows === 1) {
  92.                 $a = ['id' => $id, 'username' => $found];
  93.                 return $a;
  94.             }
  95.         }
  96.         return null;
  97.     }
  98. }
  99.  
  100. $a = new Authentication();
  101. var_dump($a->unregister("test2"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement