-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenter.php
58 lines (46 loc) · 1.59 KB
/
enter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
require_once('helpers.php');
require_once('functions.php');
require_once('connect.php');
$sql = 'SELECT * FROM `categories`';
if ($res = mysqli_query($link, $sql)) {
$categories = mysqli_fetch_all($res, MYSQLI_ASSOC);
} else {
$content = include_template('error.php', ['error' => mysqli_error($link)]);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form = $_POST;
$required_fields = ['email', 'password'];
$errors = [];
foreach ($required_fields as $field) {
if (empty($form[$field])) {
$errors[$field] = "Это поле надо заполнить";
}
}
$email = mysqli_real_escape_string($link, $form['email']);
$sql = "SELECT * FROM users WHERE email = '$email'";
$res = mysqli_query($link, $sql);
$user = $res ? mysqli_fetch_array($res, MYSQLI_ASSOC) : null;
if (!count($errors) and $user) {
if (password_verify($form['password'], $user['password'])) {
$_SESSION['user'] = $user;
} else {
$errors['password'] = 'Вы ввели неверный пароль';
}
} else {
$errors['email'] = 'Такой пользователь не найден';
}
if (count($errors)) {
$content = include_template('login.php', ['form' => $form, 'errors' => $errors]);
} else {
header("Location: /index.php");
exit();
}
} else {
$content = include_template('login.php', []);
if (isset($_SESSION['user'])) {
header("Location: /index.php");
exit();
}
}
print(include_template('layout-inner.php', ['content' => $content, 'categories' => $categories, 'title' => 'Вход']));