forked from DSCfuo/fuocribs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.php
More file actions
222 lines (189 loc) · 7.14 KB
/
signup.php
File metadata and controls
222 lines (189 loc) · 7.14 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
require_once 'db/connect.php';
require_once 'validate.php';
require_once 'include/header.php';
if (isset($_SESSION['user_id'])){
header('');
}
else {
$output_form = true;
$success_msg = false;
$fullnameErr = $usernameErr = $emailErr = $phoneErr = "";
$pwd1Err = $pwd2Err = "";
if ($_SERVER['REQUEST_METHOD'] == "POST"){
function form_err($text) {
return "<span style='display:block; margin-top: 2%; font-size: 16px; color:red'>$text</span>";
}
if (empty($_POST['fullname'])){
$fullnameErr = "fullname is required";
}
else {
if (!preg_match("/^[a-zA-Z ]+$/",$_POST['fullname'])){
$fullnameErr = form_err("Only letters and spaces are allowed");
}
else {
if (strlen($_POST['fullname']) < 3) {
$fullnameErr = form_err("Name is too short");
}
else {
$fullname = test_input($_POST['fullname']);
}
}
}
if (empty($_POST['phone'])){
$phoneErr = form_err("Phone number is required");
}
else {
if (!preg_match("/^0[789]\d{9}$/",$_POST['phone'])){
$phoneErr = form_err("Invalid phone number");
}
else {
if (strlen($_POST['fullname']) < 3) {
$phoneErr = form_err("Invalid phone number");
}
else {
$phone = test_input($_POST['phone']);
}
}
}
if (empty($_POST['username'])){
$usernameErr = form_err("Username is required");
}
else {
if (!preg_match("/^[a-zA-Z ]+[0-9]*?$/",$_POST['username'])){
$usernameErr = form_err("Only letters and numbers are allowed");
}
else if ($_POST['username'] == "admin"){
$usernameErr = form_err("Invalid username");
}
else {
if (strlen($_POST['username']) < 4) {
$usernameErr = form_err("Invalid username");
}
else {
$username= test_input($_POST['username']);
}
}
}
if (empty($_POST['email'])){
$emailErr = "Email is required";
}
else {
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$emailErr = form_err("Invalid email address");
}
else {
$email = test_input($_POST['email']);
}
}
if (empty($_POST['pwd1'])){
$pwd1Err = "Password is required";
}
else {
if (strlen($_POST['pwd1']) < 6){
$pwd1Err = form_err("Your password must contain at least 6 characters");
}
else {
$pwd1 = test_input($_POST['pwd1']);
}
}
if (empty($_POST['pwd2'])){
$pwd2Err = form_err("Password is required");
}
else {
$pwd2 = test_input($_POST['pwd2']);
}
if (isset($fullname) && isset($username) && isset($email)
&& isset($pwd1) && isset($pwd2)){
if ($pwd1 !== $pwd2){
$pwd2Err = form_err("Both passwords don't match");
$pwd1Err = "";
}
else {
//check to see if a user with the provided email or username already exists
$check_username = "SELECT *
FROM students
WHERE username = '$username';
";
$username_result = mysqli_query($conn, $check_username) or die('error checking if username already exists');
$check_email = "SELECT *
FROM students
WHERE email = '$email';
";
$email_result = mysqli_query($conn, $check_email) or die('error checking if email already exists');
if (mysqli_num_rows($username_result) == 1){
//Username already exists
$usernameErr = form_err("A user with this username already exists");
}
if (mysqli_num_rows($email_result) == 1){
//email already exists
$emailErr = form_err("A user with this email already exists");
}
if (mysqli_num_rows($username_result) < 1 && mysqli_num_rows($email_result) < 1){
//unique user
$query = "INSERT INTO students
(fullname, username, email, phone, signup_date,password)
VALUES ('$fullname', '$username', '$email', '$phone', NOW(), md5('$pwd1'));
";
mysqli_query($conn, $query) or die(mysqli_error($conn));
$output_form = false;
$success_msg = true;
}
}
}
}
}
?>
<div class="container signup-form-bg" >
<div class="col-md-4" style="color: grey; margin-top:5%; margin-left: 13%;" >
<h1>FUOCRIBS</h1>
</div>
<div class="col-md-5">
<?php if($output_form) { ?>
<form class="signup-form" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<h2>Create an account </h2>
<div class="form-group">
<label for="fullname">Full name:</label>
<input type="fullname" class="form-control" name="fullname" id="fullname" placeholder="Enter your full name"
value="<?php if (isset($fullname)) echo $fullname;?>" required>
<?php if(isset($fullnameErr)) echo $fullnameErr ?>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter your email"
value="<?php if (isset($email)) echo $email;?>" required>
<?php if(isset($emailErr)) echo $emailErr ?>
</div>
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" id="username" placeholder="Enter your username"
value="<?php if (isset($username)) echo $username;?>" required>
<?php if(isset($usernameErr)) echo $usernameErr ?>
</div>
<div class="form-group">
<label for="phone">Phone number:</label>
<input type="phone" class="form-control" name="phone" id="phone" placeholder="Enter your phone number"
value="<?php if (isset($phone)) echo $phone;?>" required>
<?php if(isset($phoneErr)) echo $phoneErr ?>
</div>
<div class="form-group">
<label for="pwd1">Password:</label>
<input type="password" class="form-control" name="pwd1" id="pwd1" placeholder="create password" required>
<?php if(isset($pwd1Err)) echo $pwd1Err ?>
</div>
<div class="form-group">
<label for="pwd2">Confirm password:</label>
<input type="password" class="form-control" name="pwd2" id="pwd2" placeholder="Enter your password again" required>
<?php if(isset($pwd2Err)) echo $pwd2Err ?>
</div>
<button type="submit" class="btn btn-default">Sign up</button>
<div class="base">
<p>Already have an account? <a href="#">Log in</a></p>
<a href="#">Forgot Password?</a>
</div>
</form>
<?php } else {?>
<p>You have successfully signed up. Please <a href='login.php'>click here to continue</a></p>
<?php } ?>
</div>
</div>