Created
January 25, 2021 11:33
-
-
Save breadlesscode/51c1f27479320ec81741f2d0c5046f91 to your computer and use it in GitHub Desktop.
Very simple and basic Session Login Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Authentication | |
{ | |
/** | |
* hashed with password_hash and self::HASH_ALGORITHM | |
*/ | |
protected $users = [ | |
'admin' => '$2y$10$ea80bTD/1qmS.HFJbSL2luYYXYhYVOkUq7J6obihEjg/p0EHzoKPm', | |
]; | |
const HASH_ALGORITHM = PASSWORD_BCRYPT; | |
public function __construct() | |
{ | |
@session_start(); | |
} | |
public function login(string $username, string $password): bool | |
{ | |
if (!isset($this->users[$username])) { | |
return false; | |
} | |
if (!password_verify($this->users[$username], $password)) { | |
return false; | |
} | |
$_SESSION['is_logged_in'] = true; | |
return true; | |
} | |
public function logout() | |
{ | |
$_SESSION['is_logged_in'] = false; | |
} | |
public function isAuthenticated() | |
{ | |
return $_SESSION['is_logged_in']; | |
} | |
public function check() | |
{ | |
return $this->isAuthenticated(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment