-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup_post.php
81 lines (62 loc) · 2.43 KB
/
signup_post.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
74
75
76
77
78
79
80
81
<?php
// connect to DB
include 'db_connexion.php';
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['confirm_password'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$confirm_password = htmlspecialchars($_POST['confirm_password']);
} else {
header('Location: signup.php?e=missing_form');
exit;
}
// Check if data is correct (email is an email, password is correct, confirm_password = password)
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (preg_match("#[a-z]|[0-9]#", $name)) {
// Password of 8 characters with at least one upercase one number and one special character
if (preg_match("/^(?=.*[!@#$%^&*-])(?=.*[0-9])(?=.*[A-Z]).{8,254}$/", $password)) {
if ($password == $confirm_password) {
// Haching password
$hash_password = password_hash($password, PASSWORD_DEFAULT);
// Upload data
$reponse = $bdd -> query('SELECT email FROM users');
$user_exists = false;
while ($donnees = $reponse->fetch()) {
//Check if user already exists
if ($email == $donnees['email']) {
$user_exists = true;
}
}
$reponse->closeCursor();
if ($user_exists) {
header('Location: signup.php?e=user_exist');
} else {
//Create new user
$req = $bdd->prepare('INSERT INTO users(name, email, password) VALUES(:name, :email, :password)');
$req->execute(array(
'name' => $name,
'email' => $email,
'password' => $hash_password
));
$user_created = true;
}
} else {
header('Location: signup.php?e=match_password');
exit;
}
} else {
header('Location: signup.php?e=bad_password');
exit;
}
} else {
header('Location: signup.php?e=name');
exit;
}
} else {
header('Location: signup.php?e=email');
exit;
}
// Go to next page
if ($user_created == true) {
header('Location: index.php?e=sucess');
}