-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.php
169 lines (144 loc) · 5.94 KB
/
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
<!DOCTYPE html>
<head>
<title>Register</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<?php include "connection.php"; ?>
<?php
session_start(); // starts a new session
$errCtr = 0;
$errMsgs = [];
// Performs validation when the form is submitted
if (isset($_POST['register'])) { // checks if the form has been submitted
// Stores what was in 'uname' (the username prompt) in variable if it's valid
$username = validUsername($_REQUEST['uname']);
// Stores what was in 'psw1' and 'psw2' (the password prompts) in variables if it's valid
$password = validPassword($_REQUEST['psw1'], $_REQUEST['psw2']);
}
?>
<body>
<div id="wrapper3" class="special">
<div id="container">
<?php
create();
/**
* Used to check if a username that was entered is valid
*
* @param String $uname represents the username that was entered
*
* @return String returns either null or a valid username
*/
function validUsername(String $uname)
{
global $errCtr, $errMsgs;
// Represents length (not including any whitespace characters such as spaces)
$lenTrimUname = strlen(str_replace(" ", "", $uname));
if (empty(trim($uname))) {
$uname = null;
$errMsgs[] = "\t<p>No username was entered. Note: Any white space characters that were entered were trimmed</p>";
$errCtr++;
} elseif ($lenTrimUname < 2) {
$uname = null;
$errMsgs[] = "\t<p>Invalid username, " . str_replace(" ", "", $uname) . " should be at least 2 characters long and all whitespace characters will be removed</p>";
$errCtr++;
} else {
// Used to remove all whitespace characters
$uname = str_replace(" ", "", $uname);
}
return $uname;
}
/**
* Used to check if a password that was entered is valid
*
* @param String $psw1 represents the password that was entered
* @param String $psw2 represents the password that was re-entered
*
* @return String returns either null or a valid password
*/
function validPassword(String $psw1, String $psw2)
{
global $errCtr, $errMsgs;
// Represents the length
$pattern = "/(?=.*[A-Z|a-z])(?=.*\d)(?=.*[\W|_])[A-Za-z\d\W_]{8,}$/";
$format1 = preg_match($pattern, $psw1) && preg_match("/^\S{8,}$/", $psw1);
$format2 = preg_match($pattern, $psw2) && preg_match("/^\S{8,}$/", $psw2);
if (empty(trim($psw1)) || empty(trim($psw2))) {
$errMsgs[] = "\t<p>Password should be entered and re-entered</p>";
$errCtr++;
$psw1 = null;
} else {
if ($psw1 != $psw2) {
$errMsgs[] = "\t<p>A different password was entered</p>";
$errCtr++;
$psw1 = null;
} elseif (!$format1 || !$format2) {
$errMsgs[] = "\t<p>Invalid password, $psw1 should be at least 8 characters long with no whitespace characters and contain at least one letter, one number, and one special chatacter</p>";
$errCtr++;
$psw1 = null;
}
}
return $psw1;
}
/**
* Checks if the user's account should be created
*
* @return void
*/
function create()
{
global $errCtr;
if ($errCtr == 0) {
$created = createUser();
if ($created) {
header("Location: http://localhost/PHPatriots/login.php");
} else {
displayErrors();
}
} else {
displayErrors();
}
}
/**
* Prints error messages and error page
*
* @return void
*/
function displayErrors()
{
global $errMsgs;
echo "\t<h1>Error</h1>\n";
reset($errMsgs);
foreach ($errMsgs as $errMsg) {
echo $errMsg;
}
echo "\t<a href='register.html'>Click here to try again</a>";
}
/**
* Finds users in the database
*
* @return bool
*/
function createUser()
{
global $username, $password, $con;
try {
// Prepare and binds placeholders
$query = "INSERT INTO Accounts (username, password) VALUES (?, ?)";
$prep_insert = $con->prepare($query);
$prep_insert->bind_param("ss", $uname, $psw);
// Sets parameters and execute prepare statement
$uname = $username;
$psw = password_hash($password, PASSWORD_DEFAULT);
$prep_insert->execute();
// End prepare statement
$prep_insert->close();
return true;
} catch (Exception $e) {
echo "<script>console.error('" . $e->getMessage() . "');</script>";
return false;
}
}
?>
</div>
</div>
</body>