-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
246 lines (220 loc) · 10.8 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html lang="en">
<?php require 'navbar.php'; ?>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Crimson+Text' rel='stylesheet' type='text/css'>
<title>Sign Up</title>
<style>
.breadcrumb{
background: none;
text-align: left;
}
.navbar-brand{
font-family: 'Crimson Text', serif;
}
body{padding-bottom:8%;}
.top-container{
margin-top: 80px;
/* background-color:#e5e5e5;*/
border-radius: 10px;
}
.transbox{
background:rgba(0, 0, 0, .07);
border-radius: 10px;
box-shadow: 1px 7px 36px -5px;
}
.error {color: #FF0000;}
</style>
</head>
<body>
<?php run_scripts_body();
static $emailNotValid;
static $passwordNotValid;
static $passwordNotMatch;
static $firstNameNotValid;
static $lastNameNotValid;
static $phoneNumberNotValid;
static $zipCodeNotValid;
static $emailRegistered;
if($_SERVER["REQUEST_METHOD"] == "POST") {
check_signup();
}
else {
$emailNotValid = false;
$passwordNotValid = false;
$passwordNotMatch = false;
$firstNameNotValid = false;
$lastNameNotValid = false;
$phoneNumberNotValid = false;
$zipCodeNotValid = false;
$emailRegistered = false;
}
?>
<div class="container top-container transbox">
<div class="container text-center">
<h1>Account Creation</h1>
</div>
<form method="post" enctype="multipart/form-data" role="form">
<!User Login Information>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputEmail">Email</label>
<span class="error"><?php
if($GLOBALS['emailNotValid']) {echo "Email not valid";}
else if($GLOBALS['emailRegistered']) {echo "Email already registered";}
?></span>
<input type="email" class="form-control" name="InputEmail" placeholder="Enter Email" value="<?php echo isset($_POST['InputEmail']) ? $_POST['InputEmail'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputPW1">New Password</label>
<span class="error"><?php
if($GLOBALS['passwordNotValid']) {echo "Password is not valid (only letters and numbers allowed)";}
?></span>
<input type="password" class="form-control" name="InputPW1" placeholder="Create Password" value="<?php echo isset($_POST['InputPW1']) ? $_POST['InputPW1'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputPW2">Re-Enter New Password</label>
<span class="error"><?php
if($GLOBALS['passwordNotMatch']) {echo "Passwords do not match";}
?></span>
<input type="password" class="form-control" name="InputPW2" placeholder="Re-Enter Password" value="<?php echo isset($_POST['InputPW2']) ? $_POST['InputPW2'] : '' ?>">
</div>
</div>
<!Contact Information>
<div class="container text-center"><h4>Contact Information</h4></div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputFirstName">First Name</label>
<span class="error"><?php
if($GLOBALS['firstNameNotValid']) {echo "First name not valid (only letters allowed)";}
?></span>
<input type="firstname" class="form-control" name="InputFirstName" placeholder="First Name" value="<?php echo isset($_POST['InputFirstName']) ? $_POST['InputFirstName'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputLastName">Last Name</label>
<span class="error"><?php
if($GLOBALS['lastNameNotValid']) {echo "Last name not valid (only letters allowed)";}
?></span>
<input type="lastname" class="form-control" name="InputLastName" placeholder="Last Name" value="<?php echo isset($_POST['InputLastName']) ? $_POST['InputLastName'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputPhone">Phone #</label>
<span class="error"><?php
if($GLOBALS['phoneNumberNotValid']) {echo "Phone number not valid";}
?></span>
<input type="phone" class="form-control" name="InputPhone" placeholder="###-###-####" value="<?php echo isset($_POST['InputPhone']) ? $_POST['InputPhone'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<label for="InputZip">Zip Code</label>
<span class="error"><?php
if($GLOBALS['zipCodeNotValid']) {echo "Zip code not valid (5 digits)";}
?></span>
<input type="zip" class="form-control" name="InputZip" placeholder="#####" value="<?php echo isset($_POST['InputZip']) ? $_POST['InputZip'] : '' ?>">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
<div class="input-group input-group-sm col-sm-offset-4 col-sm-4">
<a href="login.php">Already a User?</a>
</div> <br>
</div>
</body>
</html>
<?php
/*
*/
//checks if signup information is correct, and that the user doesn't already exist
function check_signup() {
$original_email = trim($_POST["InputEmail"]);
$clean_email = filter_var($original_email, FILTER_SANITIZE_EMAIL);
$fail = false;
// if email has special characters or doesn't have right format, exit
if ($original_email != $clean_email || !filter_var($original_email,FILTER_VALIDATE_EMAIL)) {
$GLOBALS['emailNotValid'] = true;
$fail = true;
}
// if password contains special characters, exit
if(!filter_var($_POST["InputPW1"], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z0-9_]*$/")))) {
$GLOBALS['passwordNotValid'] = true;
$fail = true;;
}
//if passwords do not match, exit
if($_POST["InputPW1"] != $_POST["InputPW2"]) {
$GLOBALS['passwordNotMatch'] = true;
$fail = true;;
}
// if first name contains any non-letter characters, exit
if(!filter_var($_POST["InputFirstName"], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z]*$/")))) {
$GLOBALS['firstNameNotValid'] = true;
$fail = true;;
}
// if last name contains any non-letter characters, exit
if(!filter_var($_POST["InputLastName"], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z]*$/")))) {
$GLOBALS['lastNameNotValid'] = true;
$fail = true;;
}
// if last name contains any non-letter characters, exit
if(!filter_var($_POST["InputPhone"], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[0-9-]*$/")))) {
$GLOBALS['phoneNumberNotValid'] = true;
$fail = true;;
}
// if last name contains any non-letter characters, exit
if(!filter_var($_POST["InputZip"], FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[0-9]{5}$/")))) {
$GLOBALS['zipCodeNotValid'] = true;
$fail = true;;
}
// if email is already registered, exit
$connection = connect_to_mysql();
$query = "SELECT * FROM users WHERE email = '" . $original_email . "'";
$row = mysqli_query($connection, $query);
echo "Num rows: " . mysqli_num_rows($row);
if(mysqli_num_rows($row) != 0) {
$GLOBALS['emailRegistered'] = true;
$fail = true;
}
close_mysql_connection($connection);
if(!$fail) {
create_user();
}
}
/*
* @var string $password the password, hashed with default php algorithm
*/
// creates the user in the DB, with the already verified and sanitized information
function create_user() {
// hashes the password to store it safely in the DB
$password = password_hash($_POST["InputPW1"], PASSWORD_DEFAULT);
$connection = connect_to_mysql();
// query to create a new user in the DB
$query = "INSERT INTO users (email,password,user_type,zip_code,phone_number,first_name,last_name)";
$query .="VALUES(";
$query .="'{$_POST["InputEmail"]}',";
$query .= "'{$password}',";
$query .= "1,";
$query .= "{$_POST["InputZip"]},";
$query .= "'{$_POST["InputPhone"]}',";
$query .= "'{$_POST["InputFirstName"]}',";
$query .= "'{$_POST["InputLastName"]}')";
if(mysqli_query($connection, $query) == FALSE) {
echo "Failed to create user";
}
close_mysql_connection($connection);
}
?>