-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_action.php
213 lines (184 loc) · 6.81 KB
/
registration_action.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
<?php
include("{$_SERVER['DOCUMENT_ROOT']}/config/database.php");
if(!isset($_SESSION['SessionStarted']))
{
session_start();
$_SESSION['SessionStarted'] = 1;
}
if(!isset($_POST['Submit']))
{
include("{$_SERVER['DOCUMENT_ROOT']}/header.php");
print("<span class=\"error\">Invalid request. Please return to another area of the forum.</span>");
}
else
{
?>
<?php
extract($_POST);
$NUM_FIELDS = 6;
$username_search_pattern = "/^[_0-9a-z]{3,20}$/i";
$password_search_pattern = "/^[_0-9a-z-]{6,20}$/i";
$firstname_search_pattern = "/^[a-z]{1}[a-z-]{1,19}$/i";
$lastname_search_pattern = "/^[a-z]{1}[a-z-]{1,19}$/i";
$email_search_pattern = "/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$/i";
// set up some flags to make upcoming if statements easier
if($UserName == "")
$UserNameIsBlank = 1;
else
$UserNameIsBlank = 0;
if($Password == "")
$PasswordIsBlank = 1;
else
$PasswordIsBlank = 0;
if($ConfirmPassword == "")
$ConfirmPasswordIsBlank = 1;
else
$ConfirmPasswordIsBlank = 0;
if($FirstName == "")
$FirstNameIsBlank = 1;
else
$FirstNameIsBlank = 0;
if($LastName == "")
$LastNameIsBlank = 1;
else
$LastNameIsBlank = 0;
if($EmailAddr == "")
$EmailAddrIsBlank = 1;
else
$EmailAddrIsBlank = 0;
$AllFields[0] = $UserNameIsBlank;
$AllFields[1] = $PasswordIsBlank;
$AllFields[2] = $ConfirmPasswordIsBlank;
$AllFields[3] = $FirstNameIsBlank;
$AllFields[4] = $LastNameIsBlank;
$AllFields[5] = $EmailAddrIsBlank;
$AllFieldsAreFilled = 1;
for($i=0; $i<$NUM_FIELDS; $i++)
{
if($AllFields[$i] == 1)
$AllFieldsAreFilled = 0;
}
$RegistrationError = 0;
// at this point the user is not logged in
$_SESSION['LoggedIn'] = 0;
// if all fields are blank
if($UserNameIsBlank && $PasswordIsBlank && $ConfirmPasswordIsBlank && $FirstNameIsBlank && $LastNameIsBlank && $EmailAddrIsBlank)
{
$RegistrationError = 1;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if one or more fields are blank
if(!$AllFieldsAreFilled)
{
$RegistrationError = 2;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if UserName format is incorrect
if (!preg_match($username_search_pattern, $UserName))
{
$RegistrationError = 3;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if UserName format is correct but is a duplicate of an existing UserName
else
{
$queryCheckDuplicateUsername = "SELECT UserName FROM users WHERE UserName = '".$UserName."'";
if(mysql_fetch_row(mysql_query($queryCheckDuplicateUsername)))
{
$RegistrationError = 4;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
}
// if Password format is incorrect
if (!preg_match($password_search_pattern, $Password))
{
$RegistrationError = 5;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if Password format is correct but does not match Confirm Password
else if($Password != $ConfirmPassword)
{
$RegistrationError = 6;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if FirstName format is incorrect
if (!preg_match($firstname_search_pattern, $FirstName))
{
$RegistrationError = 7;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if LastName format is incorrect
if (!preg_match($lastname_search_pattern, $LastName))
{
$RegistrationError = 8;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if EmailAddr format is incorrect
if (!preg_match($email_search_pattern, $EmailAddr))
{
$RegistrationError = 9;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
// if Email Address format is correct but is a duplicate of an existing Email Address
else
{
$queryCheckDuplicateEmailAddr = "SELECT EmailAddr FROM users WHERE EmailAddr = '".$EmailAddr."'";
if(mysql_fetch_row(mysql_query($queryCheckDuplicateEmailAddr)))
{
$RegistrationError = 10;
$_SESSION['RegistrationError'] = $RegistrationError;
die(include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
}
// get date and time to use for OrigSignup:
$now = getdate();
// set up variables:
$year = $now['year'];
$month = $now['mon'];
$day = $now['mday'];
$hours = $now['hours'];
$minutes = $now['minutes'];
$seconds = $now['seconds'];
$OrigSignup = "$year-$month-$day $hours:$minutes:$seconds";
$LastLogin = $OrigSignup;
$SubscribeEmail = 0;
// add info from text fields and getDate to the database (vars come from POST input from registraion.php, except OrigSignup and LastLogin)
$queryAdd = "INSERT INTO users VALUES(NULL, '$UserName', MD5('$Password'), '$FirstName', '$LastName', '$EmailAddr', '$OrigSignup', '$LastLogin', '$SubscribeEmail')";
if(!($result = mysql_query($queryAdd)))
{
$RegistrationError = 11;
$_SESSION['RegistrationError'] = $RegistrationError;
die(mysql_error() . include("{$_SERVER['DOCUMENT_ROOT']}/registration_action_error.php")); // terminate script execution
}
else
{
$RegistrationError = 0;
$_SESSION['RegistrationError'] = $RegistrationError;
$queryGetUserID = "SELECT UserID FROM users WHERE UserName = '".$UserName."'";
$row = (mysql_fetch_row(mysql_query($queryGetUserID)));
$UserID = $row[0];
$_SESSION['UserID'] = $UserID;
$_SESSION['UserName'] = $UserName;
$_SESSION['LoggedIn'] = 1;
$_SESSION['EmailAddr'] = $EmailAddr;
$_SESSION['OrigSignup'] = $OrigSignup;
$_SESSION['LastLogin'] = $LastLogin;
// set this to 1 now that the user has logged in (by way of registration)
include("{$_SERVER['DOCUMENT_ROOT']}/header.php");
print("<span class=\"welcome\">Congratulations, <strong>$FirstName</strong>, you have successfully registered!<br/>");
$_SESSION['LoggedIn'] = 1;
}
?>
<?php
} // end else from top of file if statement
include("{$_SERVER['DOCUMENT_ROOT']}/footer.php");
?>