-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_create.php
252 lines (213 loc) · 9.2 KB
/
account_create.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
247
248
249
250
251
252
<html>
<?php
require('connect-db.php');
?>
<style>
.error_message { color: crimson; font-style:italic; }
</style>
<head>
<title>Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta name="author" content="Alan Zhai">
<meta name="description" content="Account Creation">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- <link rel="stylesheet" href="../styles/account_create.css"> -->
</head>
<?php include "./navbar.php" ?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//creating the user in the database
$user = $_POST['username1'];
// $pass = password_hash($_POST['pass1'], PASSWORD_BCRYPT);
$pass = $_POST['pass1'];
$first = $_POST['first_name'];
$last = $_POST['last_name'];
$email = $_POST['Email1'];
$phone = (int)$_POST['phone'];
$query = "SELECT * FROM login WHERE username = :username";
$statement = $db->prepare($query);
$statement->bindParam(':username', $user);
$statement->execute();
$results = $statement->fetchAll();
$check = sizeof($results);
$statement->closecursor();
//making sure that the user doesn't exist in the database already
if ($check == 0)
{
$_POST['taken'] = 0;
$query = "INSERT INTO users (username, role, first_name, last_name, email_address, phone_num)
VALUES (:username, :role, :first_name, :last_name, :email_address, :phone_num)";
$statement = $db->prepare($query);
$statement->bindValue(':username', $user);
$statement->bindValue(':role', 'guest');
$statement->bindValue(':first_name', $first);
$statement->bindValue(':last_name', $last);
$statement->bindValue(':email_address', $email);
$statement->bindValue(':phone_num', $phone);
$statement->execute();
$statement->closeCursor();
$query = "INSERT INTO login (username, password) VALUES (:username, :password)";
$statement = $db->prepare($query);
$statement->bindValue(':username', $user);
$hashed_password = password_hash($pass, PASSWORD_BCRYPT);
$statement->bindValue(':password', $hashed_password);
$statement->execute();
$statement->closeCursor();
echo "<script>
alert('Account Created');
window.location.href='home.php';
</script>";
}
}
?>
<div class="container" style="text-align: center;">
<!-- one of the columns in the container -->
<!-- the sign up column -->
<h3>
Sign Up
</h3>
<!-- registration form -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="RegisterForm" method="post" onsubmit="return checkRegistration()">
<div class="form-group">
<label for="exampleInputUsername">Username</label>
<input type="text" class="form-control" name="username1" id="username1" placeholder="Enter username" >
<!-- <span class="error_message" id="msg_user"><?php if(!empty($user)) echo "Username already taken, please
enter another"?></span> -->
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="pass1" id="pass1" placeholder="Password" >
<span class="error_message" id="msg_pass"></span>
</div>
<div class="form-group">
<label for="exampleInputPassword2">Re-enter Password</label>
<input type="password" class="form-control" name="pass2" id="pass2" placeholder="Password" >
</div>
<div class="form-group">
<div class="form-row">
<div class = "col">
<label for="first_name">First Name</label>
<input type="text" class="form-control" name="first_name" id="first_name" placeholder="Enter your first name"
value="<?php if(!empty($user)) echo $_POST['first_name']?>">
</div>
<div class = "col">
<label for="last_name">Last Name</label>
<input type="text" class="form-control" name="last_name" id="last_name" placeholder="Enter your last name"
value="<?php if(!empty($user)) echo $_POST['last_name']?>">
</div>
</div>
<span class="error_message" id="msg_name"></span>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="Email1" id="Email1" placeholder="Enter email"
value="<?php if(!empty($user)) echo $_POST['Email1']?>">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
<span class="error_message" id="msg_email"></span>
</div>
<div class ="form-group">
<label for="phone">Phone Number</label>
<input type="text" class = "form-control" name="phone" id="phone" placeholder="Enter your phone number"
value="<?php if(!empty($user)) echo $_POST['phone']?>">
<small id="Phone number help" class="form-text text-muted">Please enter a 10 digit phone number</small>
<span class="error_message" id="msg_phone"></span>
</div>
<div>
<span class="error_message" id="overall"></span>
</div>
<button type="submit" class="btn btn-secondary">Submit</button>
</form>
</div>
</div>
<!-- type checking and form validation using javascript -->
<script>
// checking that the phonenumber is a string
function isInt(str)
{
var val = parseInt(str);
return (val > 0);
}
// checking that the email is an email
function isEmail(str)
{
var re = /\S+@\S+\.\S+/;
var match_test = re.test(str);
return match_test;
}
//making sure that all elements are filled out
function checkRegistration()
{
var username = document.getElementById("username1").value;
var pass1 = document.getElementById("pass1").value;
var pass2 = document.getElementById("pass2").value;
var first = document.getElementById("first_name").value;
var last = document.getElementById("last_name").value;
var email = document.getElementById("Email1").value;
var phone = document.getElementById("phone").value;
var errors = 0;
if (username == "")
{
errors++;
document.getElementById("msg_user").innerHTML = "Please enter a valid username";
}
else
{
document.getElementById("msg_user").innerHTML = "";
}
// //check if passwords match
if (pass1 == "" || pass2 == "")
{
errors++;
document.getElementById("msg_pass").innerHTML = "Your passwords cannot be empty";
}
else if(pass1 != pass2)
{
errors++;
document.getElementById("msg_pass").innerHTML = "Your passwords must match";
}
else
{
document.getElementById("msg_pass").innerHTML = "";
}
if (first == "" || last == "")
{
errors++;
document.getElementById("msg_name").innerHTML = "Your firstname or lastname cannot be empty";
}
if ( !isEmail(email))
{
errors++;
document.getElementById("msg_email").innerHTML = "Please enter a valid email address";
}
else
{
document.getElementById("msg_email").innerHTML = "";
}
if ( !isInt(phone))
{
errors++;
document.getElementById("msg_phone").innerHTML = "Your phone is not an integer or it is blank";
}
else if (phone.length != 10) {
errors++;
document.getElementById("msg_phone").innerHTML = "Please enter a 10 digit phone number";
}
else
{
document.getElementById("msg_phone").innerHTML = "";
}
if (errors > 0)
{
document.getElementById("overall").innerHTML = "Please fix the errors and then resubmit";
return false;
}
else
{
document.getElementById("overall").innerHTML = "";
return true;
}
}
</script>
</html>