-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
187 lines (155 loc) · 8.01 KB
/
signup.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
require ("functions.php");
//check if the user has posted the form
if($_SERVER['REQUEST_METHOD'] == "POST"){
//get the values
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
//check if email is in the right format
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$email = $_POST['email'];
}else{
//error message if email is not valid
$invalidEmail = "This email is not valid";//declare a variable that tells that email is invalid
}
$birthday = $_POST['birthday'];
$mobile = $_POST['mobile'];
$adress = $_POST['adress'];
$pass = $_POST['pass'];
$passConfig = $_POST['passConfig'];
//Check if password is shorter than 8 chars
if(strlen($pass) < 8){
$invalidPass = "Password must be at least 8 characters long."; //declare a variable that tells that password is invalid
}
// chechk if passwords match
if(strcmp($pass, $passConfig)){
//passwords dont match
if(isset($invalidPass)){
//concatenate the invalidPass string so the user will see both errors relating to the password
$invalidPass .=" Passwords don't match.";
}
else{
//let the user know that the passwords dont match
$invalidPass = "Passwords don't match.";
}
}
//Check if the inputs required are empty and email and password are valid
if(!empty($firstname) && !empty($lastname) && !empty($pass) && !empty($passConfig)
&& !isset($invalidEmail) && !isset($invalidPass)){
//db connection
$dbc = dbConn();
//Check if the user exists in the database
if(!checkUser($email)){
//Insert users into db if the user isn't already signed up
$insertUsers = "INSERT INTO users
(first_name, last_name, birthday, mobile, email, password, adress)VALUES
('$firstname', '$lastname', '$birthday', '$mobile', '$email', '$pass', '$adress')";
//Check if user was inserted into users table
if(mysqli_query($dbc, $insertUsers)){
//If the user is inserted we will collect the user_Id from db so we can store that in users_roles table
$userData = checkUser($email);
$userId = $userData['user_Id'];
//Insert the user_Id into db table users_roles
$insertUserRoleQuery = "INSERT INTO users_roles(role_Id, user_Id) VALUES(1, $userId)";
//Check if the insert in the users_roles was successful
if(mysqli_query($dbc, $insertUserRoleQuery)){
$userInserted = "You were signed up successfully! You can now log in.";
}
}
}else{
$userTaken = "You already have an account. You can log in";
}
//close the db connection
mysqli_close($dbc);
}else{
$required = "*";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MusicMania-SignUp</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="d-flex min-vh-100 justify-content-center align-items-center">
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> " class="pe-2 ps-2 col-md-4 d-flex flex-column justify-content-evenly">
<?php if(isset($required)){echo "<span class = \"text-danger align-self-center\">Please fill all the required fields</span>";} ?>
<?php if(isset($userInserted)){echo "<span class = \"text-success align-self-center\">$userInserted</span>";} ?>
<?php if(isset($userTaken)){echo "<span class = \"text-warning align-self-center\">$userTaken</span>";} ?>
<div class="d-flex justify-content-evenly mb-3">
<label for="firstname" class="form-label align-self-center mb-0 me-auto">First Name:</label>
<input class="form-control w-auto" type="text"
value="<?php if(isset($firstname) && !isset($userInserted) && !isset($userTaken))
{echo $firstname;} ?>"
name="firstname">
<div>
<?php if(isset($required)){echo "<span class = \"align-middle text-danger\">$required</span>";} ?>
</div>
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="lastname" class="form-label align-self-center mb-0 me-auto">Last Name:</label>
<input type="text" class="form-control flex-grow w-auto"
value="<?php if(isset($lastname) && !isset($userInserted) && !isset($userTaken))
{echo $lastname;} ?>"
name="lastname">
<div>
<?php if(isset($required)){echo "<span class = \"align-middle text-danger\">$required</span>";} ?>
</div>
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="email" class="form-label align-self-center mb-0 me-auto">Email:</label>
<input type="text" class="form-control w-auto"
value="<?php if(isset($email) && !isset($userInserted) && !isset($userTaken)){echo $email;} ?>"
name="email">
<div>
<?php if(isset($required)){echo "<span class = \"align-middle text-danger\">$required</span>";} ?>
</div>
</div>
<?php if(isset($invalidEmail)){echo "<span class=\"text-danger\">$invalidEmail</span>"; }?>
<div class="d-flex justify-content-evenly mb-3">
<label for="birthday" class="form-label align-self-center mb-0 me-auto">Birthday:</label>
<input type="date" class="form-control w-auto"
value="<?php if(isset($birthday) && !isset($userInserted) && !isset($userTaken)){echo $birthday;} ?>"
name="birthday">
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="mobile" class="form-label align-self-center mb-0 me-auto">Mobile:</label>
<input type="tel" class="form-control w-auto"
value="<?php if(isset($mobile) && !isset($userInserted) && !isset($userTaken)){echo $mobile;} ?>"
name="mobile">
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="adress" class="form-label align-self-center mb-0 me-auto">Adress:</label>
<input type="text" class="form-control w-auto"
value="<?php if(isset($adress) && !isset($userInserted) && !isset($userTaken)){echo $adress;} ?>"
name="adress">
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="pass" class="form-label align-self-center mb-0 me-auto">Password:</label>
<input type="password" class="form-control w-auto" name="pass">
<div>
<?php if(isset($required)){echo "<span class = \"align-middle text-danger\">$required</span>";} ?>
</div>
</div>
<div class="d-flex justify-content-evenly mb-3">
<label for="passConfig" class="form-label align-self-center mb-0 me-auto">Confirm Password:</label>
<input type="password" class="form-control w-auto" name="passConfig">
<div>
<?php if(isset($required)){echo "<span class = \"align-middle text-danger\">$required</span>";} ?>
</div>
</div>
<?php if(isset($invalidPass)){ echo "<span class = \"text-danger align-self-center\">$invalidPass</span>";} ?>
<div class="d-flex justify-content-evenly align-items-center mb-3">
<input type="submit" class="btn-primary rounded" id="signUp" value="Sign Up">
<a href="login.php" class="link-secondary">Login</a>
</div>
</form>
</div>
</body>
</html>