SHARE
TWEET

Untitled

a guest Apr 20th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class LoginModel extends Model {
  4.     private $db;
  5.  
  6.     function __construct() {
  7.         $this->db = $this->dbConnection();
  8.     }
  9.  
  10.     public function checkCredentials($identity, $password) {
  11.         if($identity == null || $password == null) {
  12.             $error = "Missing Details!";
  13.             return $error;
  14.         } else {
  15.             return true;
  16.         }
  17.     }
  18.  
  19.     public function tryLogin($identity, $password) {
  20.         try {
  21.             $stmt = $this->db->prepare("SELECT * FROM admins WHERE admin_name=:identity");
  22.             $stmt->execute(array(":identity"=>$identity));
  23.             $result = $stmt->fetch(PDO::FETCH_ASSOC);
  24.             if($result > 0) {
  25.                 if(password_verify($password, $result['admin_password'])) {
  26.                     return true;
  27.                 } else {
  28.                     $error = "Wrong Details!";
  29.                     return $error;
  30.                 }
  31.             } else {
  32.                 $error = "Wrong Details!";
  33.                 return $error;
  34.             }
  35.         } catch(PDOException $exception) {
  36.             echo $exception->getMessage();
  37.         }
  38.     }
  39.  
  40.     public function login($identity) {
  41.         try {
  42.             $stmt = $this->db->prepare("SELECT * FROM admins WHERE admin_name=:identity");
  43.             $stmt->execute(array(":identity"=>$identity));
  44.             $result = $stmt->fetch(PDO::FETCH_ASSOC);
  45.             $_SESSION['admin_identity'] = hash('sha256', openssl_random_pseudo_bytes(16));
  46.             $stmt = $this->db->prepare("UPDATE admins SET admin_token=:token WHERE admin_name=:identity");
  47.             $stmt->execute(array(":token"=>$_SESSION['admin_identity'], ":identity"=>$identity));
  48.         } catch(PDOException $exception) {
  49.             echo $exception->getMessage();
  50.         }
  51.     }
  52.  
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top