Created
February 23, 2018 18:20
-
-
Save Sentinel17/f46f008829c55425708ea984c2355cc4 to your computer and use it in GitHub Desktop.
Домашнее задание №10
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 | |
include 'Database.php'; | |
class Auth { | |
protected $pdo; | |
public function __construct(){ | |
$db = new Database(); | |
$db->baseConfig(); | |
$this->pdo = $db->pdo; | |
} | |
public function login($login, $password){ | |
$stmt = $this->pdo->prepare('SELECT * FROM users WHERE login = :login and password = :password'); | |
$stmt->execute(array('login' => $login, 'password' => md5($password))); | |
$user = $stmt->fetch(); | |
if($user) { | |
$this->updateAuthTime($user['id']); | |
} | |
return $user; | |
} | |
public function updateAuthTime($user_id){ | |
$stmt = $this->pdo->prepare('UPDATE users SET lastVisit = :lastVisit WHERE id=:id'); | |
$result = $stmt->execute(array('id' => $user_id, 'lastVisit' => date('Y-m-d H:i:s'))); | |
return $result; | |
} | |
} | |
?> |
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 | |
header('Content-Type: text/html; charset=utf-8'); | |
include 'Auth.php'; | |
session_start(); | |
if(isset($_POST['enter'])){ | |
$data = array_map('trim', $_POST); | |
$data = array_map('strip_tags', $data); | |
$login = $data['login']; | |
$password = $data['password']; | |
$auth = new Auth(); | |
$user = $auth->login($login, $password); | |
$lastVisit = $auth->updateAuthTime($user_id); | |
if($user){ | |
$_SESSION['auth'] = true; | |
$_SESSION['user'] = $user; | |
echo 'Авторизовались' . '<br/>'; | |
} | |
else {?> | |
<p style="color:red;">Неверный логин/пароль</p> | |
<?php } | |
} | |
if (!isset($_SESSION['auth'])) : | |
?> | |
<form action = "" method = "POST"> | |
<input name = "login" type = "text" reqiured/> | |
<input name = "password" type = "password" reqiured/> | |
<input type = "submit" name="enter"/> | |
</form> | |
<?php else:?> | |
Добро пожаловать, <?php echo $_SESSION['user']['login'];?> | |
<p><a href = "Profile.php">Ваш профиль</a></p> | |
<p><a href = "Logout.php">Выйти</a></p> | |
<?php endif;?> |
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 Database { | |
protected $host ; | |
protected $charset; | |
protected $db; | |
protected $user; | |
protected $pass; | |
public function __construct(){ | |
$this->host = 'localhost'; | |
$this->charset = 'utf8'; | |
$this->db = 'test'; | |
$this->user = 'root'; | |
$this->pass = ''; | |
} | |
public function baseConfig(){ | |
$dsn="mysql:host=$this->host;dbname=$this->db;"; | |
$opt = array ( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC | |
); | |
$this->pdo = new PDO($dsn, $this->user, $this->pass); | |
} | |
} | |
?> |
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 | |
session_start(); | |
session_destroy(); | |
header('Location: AuthUser.php'); | |
?> |
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 | |
header('Content-Type: text/html; charset=utf-8'); | |
session_start(); | |
if (!isset($_SESSION['auth'])){ | |
echo 'Доступ закрыт!'; | |
die; | |
} else { | |
$user = $_SESSION['user']; | |
?> | |
<h2>Ваш профиль</h2> | |
<p>Логин: <?php echo $user['login'];?></p> | |
<p>Пароль: <?php echo $user['password'];?></p> | |
<p>ФИО: <?php echo $user['fio'];?></p> | |
<p>email: <?php echo $user['email'];?></p> | |
<p>Адрес: <?php echo $user['adress'];?></p> | |
<p>Дата последнего входа: <?php echo $_SESSION['user']['lastVisit'];?></p> | |
<p><a href = "Logout.php">Выйти</a></p> | |
<?php }?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment