Created
May 7, 2017 15:56
-
-
Save DmitryDmitrienko/24919fb7b627e7104aa7764207108238 to your computer and use it in GitHub Desktop.
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
<? | |
class AuthExceptionLogin extends Exception{ | |
public function __construct($message = "Логина нет в системе", $code = 0x100001, Throwable $previous = null) | |
{ | |
parent::__construct($message, $code, $previous); | |
} | |
} | |
class AuthExceptionPassword extends Exception{ | |
public function __construct($message = "Неверный пароль", $code = 0x100002, Throwable $previous = null) | |
{ | |
parent::__construct($message, $code, $previous); | |
} | |
} | |
class AuthClass{ | |
public $session_name = false; | |
private $authData = array( | |
'chexov' => '123456', | |
'test' => '909090' | |
); | |
public function sessionStart($name = false) | |
{ | |
if($name){ | |
$this->session_name = $name; | |
session_name($name); | |
} | |
if(!session_start()){ | |
echo 'Не удалось стартовать сессию'; | |
} | |
} | |
public function auth($login, $password) | |
{ | |
$login = htmlspecialchars($login); | |
$password = htmlspecialchars($password); | |
if(isset($this->authData[$login])){ | |
$passwordBase = $this->authData[$login]; | |
if($passwordBase === $password){ | |
$this->setAuth($login, $password); | |
} else{ | |
throw new AuthExceptionPassword(); | |
} | |
} else{ | |
throw new AuthExceptionLogin(); | |
} | |
} | |
public function setAuth($login, $password) | |
{ | |
if($login && $password){ | |
$_SESSION['is_auth'] = true; | |
$_SESSION['password'] = md5($password); | |
$_SESSION['login'] = $login; | |
} | |
session_write_close(); | |
} | |
public function logout() | |
{ | |
$_SESSION = array(); | |
session_destroy(); | |
header("Location: ?is_exit=0"); //Редирект после выхода | |
} | |
public function isAuth() | |
{ | |
return isset($_SESSION['is_auth']) && $_SESSION['is_auth'] == true; | |
} | |
public function getLogin() | |
{ | |
if($this->isAuth() && isset($_SESSION['login'])){ | |
return $_SESSION['login']; | |
} | |
return false; | |
} | |
public function main() | |
{ | |
$this->sessionStart('FEELK_SESSION'); | |
if($_POST){ | |
if(isset($_POST['enter'])){ // хотят войти | |
try{ | |
$this->auth($_POST['login'], $_POST['password']); | |
}catch (AuthExceptionLogin $e){ | |
echo $e->getMessage(); | |
} | |
catch (AuthExceptionPassword $e){ | |
echo $e->getMessage(); | |
} | |
} elseif (isset($_POST['logout'])){ | |
$this->logout(); | |
} | |
} | |
} | |
} | |
?> | |
<? | |
$s = new AuthClass(); | |
$s->main(); | |
?> | |
<form action="index.php" method="post"> | |
<?if(!$s->isAuth()):?> | |
<p>Логин: <input type="text" name="login" placeholder="Введите логин" value=""></p> | |
<p>Пароль: <input type="password" name="password" placeholder="Введите пароль" value=""></p> | |
<?else:?> | |
<?echo 'Здравствуйте: '.$s->getLogin();?> | |
<?endif;?> | |
<div class="btns"> | |
<?if(!$s->isAuth()):?> | |
<input type="submit" value="Войти" name="enter"> | |
<?else:?> | |
<input type="submit" value="Выйти" name="logout"> | |
<?endif;?> | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment