forked from M2rk13/PHP-Professional-Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg.php
73 lines (62 loc) · 1.93 KB
/
reg.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once __DIR__ . '/libs/base.php';
if (isset($_SESSION['id'])) {
header("Location: feed.php");
}
$validation_rules = [
'email' => 'filled|correct_email|exists:users,email',
'login' => 'filled|length:3,25',
'password' => 'filled|repeat_password|length:6,15',
'password-repeat' => 'filled|repeat_password',
];
$form_error_codes = [
'email' => 'Email',
'login' => 'Логин',
'password' => 'Пароль',
'password-repeat' => 'Повтор пароля',
];
$form = [
'values' => [],
'errors' => [],
];
$img_folder = __DIR__ . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR;
$title = $site_name . ': Регистрация';
if (count($_POST) > 0) {
$form['values'] = $_POST;
$form['values']['userpic-file'] = $_FILES['userpic-file'];
$form['errors'] = validate($form['values'], $validation_rules, $connection);
$form['errors'] = array_filter($form['errors']);
if (empty($form['errors'])) {
$current_time = date('Y-m-d H:i:s');
$password_hash = password_hash($_POST['password'], PASSWORD_DEFAULT);
$avatar = save_image('userpic-file', $img_folder);
secure_query_bind_result($connection,
"INSERT into users SET username = ?, email = ?, password = ?, avatar = ?, dt_add =?", false,
$_POST['login'],
$_POST['email'],
$password_hash,
$avatar,
$current_time
);
$post_id = mysqli_insert_id($connection);
$URL = '/';
header("Location: $URL");
}
}
$form_values = $form['values'] ?? [];
$page_content = include_template(
'reg-template.php',
[
'form_values' => $form['values'] ?? [],
'form_errors' => $form['errors'] ?? [],
'form_error_codes' => $form_error_codes,
]
);
$layout_content = include_template(
'layout.php',
[
'content' => $page_content,
'title' => $title,
]
);
print($layout_content);