-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_account.php
284 lines (230 loc) · 8.83 KB
/
process_account.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Account</title>
<?php include_once "include/head.inc.php" ?>
<link rel="stylesheet" href="css/admin.css">
<script src="js/admin.js" defer></script>
</head>
<body>
<?php
include_once "include/nav.inc.php";
//include_once "include/dbcon.inc.php";
?>
<?php
$fname = $errorMsg = "";
$success = true;
//if (empty($_POST["lname"])) {
// $errorMsg .= "Last name is required.<br>";
// $success = false;
//} else {
$fname = sanitize_input($_POST["fname"]);
// Additional check to make sure e-mail address is well-formed.
// if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// $errorMsg .= "Invalid email format.";
// $success = false;
// }
//}
if ($success) {
} else {
echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
$lname = $errorMsg = "";
$success = true;
if (empty($_POST["lname"])) {
$errorMsg .= "Last name is required.<br>";
$success = false;
} else {
$lname = sanitize_input($_POST["lname"]);
// Additional check to make sure e-mail address is well-formed.
// if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// $errorMsg .= "Invalid email format.";
// $success = false;
// }
}
if ($success) {
// echo "<h4>Change successful!</h4>";
} else {
echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
$email = $errorMsg = "";
$success = true;
if (empty($_POST["email"])) {
$errorMsg .= "Old email is required.<br>";
$success = false;
} else {
$email = sanitize_input($_POST["email"]);
// Additional check to make sure e-mail address is well-formed.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errorMsg .= "Invalid old email format.";
$success = false;
}
}
if ($success) {
// echo "<h4>Change successful!</h4>";
} else {
// echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
$new_email = $errorMsg = "";
$success = true;
if (empty($_POST["new_email"])) {
$errorMsg .= "New email is required.<br>";
$success = false;
} else {
$new_email = sanitize_input($_POST["new_email"]);
// Additional check to make sure e-mail address is well-formed.
if (!filter_var($new_email, FILTER_VALIDATE_EMAIL)) {
$errorMsg .= "Invalid new email format.";
$success = false;
}
}
if ($success) {
// echo "<h4>Change successful!</h4>";
} else {
// echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
//Helper function that checks input for malicious or unwanted content.
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$pwd = $errorMsg = "";
$success = true;
if (empty($_POST["pwd"])) {
$errorMsg .= "Password is required.<br>";
$success = false;
}// else {
$pwd = password_hash($_POST["pwd"], PASSWORD_DEFAULT);
// Additional check to make sure e-mail address is well-formed.
// if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// $errorMsg .= "Invalid email format.";
// $success = false;
// }
//}
if ($success) {
// echo "<h4>Change successful!</h4>";
// echo "<p>New password: " . $pwd;
} else {
// echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
$pwd_confirm = $errorMsg = "";
$success = true;
if (empty($_POST["pwd_confirm"])) {
$errorMsg .= "Confirm Password is required.<br>";
$success = false;
}// else {
$pwd_confirm = password_hash($_POST["pwd_confirm"], PASSWORD_DEFAULT);
// Additional check to make sure e-mail address is well-formed.
// if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// $errorMsg .= "Invalid email format.";
// $success = false;
// }
//}
if (($_POST["pwd"]) != ($_POST["pwd_confirm"])) {
$errorMsg .= "Password does not match Confirm Password.<br>";
$success = false;
}
if ($success) {
// echo "<h4>Change successful!</h4>";
// echo "<p>Confirm new password: " . $pwd_confirm;
} else {
// echo "<h4>The following input errors were detected:</h4>";
echo "<p>" . $errorMsg . "</p>";
}
?>
<?php
/*
* Helper function to write the member data to the DB
*/
function saveMemberToDB() {
global $fname, $lname, $email, $new_email, $pwd, $errorMsg, $success;
// Create database connection.
//include_once "include/dbcon.inc.php";
//$config = parse_ini_file('../private/db-config.ini');
//$conn = new mysqli(
// $config['servername'],
// $config['username'],
// $config['password'],
// $config['dbname']);
// TEST require_once "../include/dbcon.inc.php";
//
$config = parse_ini_file('/var/www/private/db-config.ini');
$conn = new mysqli($config['servername'], $config['username'],
$config['password'], $config['dbname']);
// Check connection
//echo "<p>" . $fname . "</p>";
// echo "<p>" . $lname . "</p>";
// echo "<p>" . $email . "</p>";
// echo "<p>" . $pwd . "</p>";
// echo "<p>" . $new_email . "</p>";
//echo "<p>" . $config['servername'] . "</p>";
if ($conn->connect_error) {
$errorMsg = "Connection failed: " . $conn->connect_error;
$success = "false";
echo "<p>" . $errorMsg . "</p>";
} else
{
// echo "<p>conn variable:" . $conn->servername . "</p>";
// echo "<p> else statement ran </p>";
// Prepare the statement:
//$stmt = $conn->prepare("INSERT INTO world_of_pets.world_of_pets_members (fname, lname, email, password) VALUES ($fname, $lname, $email, $pwd)");
// $stmt = $conn->prepare("INSERT INTO world_of_pets_members (fname, lname,
//email, password) VALUES (?, ?, ?, ?)");
//$sql = "update shophp.users set fname='ac', lname='ac', email='[email protected]' where email='[email protected]'";
//$stmt = $conn->prepare($sql);
//$stmt->execute();
//$stmt = $conn->prepare("UPDATE shophp.users SET fname=?, lname=?, email=?, password=? WHERE email=?");
//$stmt = $conn->prepare("UPDATE users SET fname=?, lname=?, email=?, password=? WHERE email=?");
$stmt = $conn->prepare("UPDATE users SET fname=?, lname=?, password=?, email=? WHERE email=?");
//update shophp.users set fname='ab', lname='ab', username='ab' where email='[email protected]'
//update shophp.users set fname='ac', lname='ac', email='[email protected]' where email='[email protected]'
//TESTING $stmt = $conn->prepare("UPDATE users SET fname='MRBEAN' WHERE email='[email protected]'");
// Bind & execute the query statement:
$stmt->bind_param("sssss", $fname, $lname, $pwd, $new_email, $email);
//$stmt->bind_param("ss", $fname, $email);
//$stmt->execute();
if (!$stmt->execute()) {
$errorMsg = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
$success = false;
echo "<p>" . $errorMsg . "</p>";
}
else
{
$stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $new_email);
$stmt->execute();
$checkrecord = $stmt->get_result();
if ($checkrecord->num_rows > 0) {
echo "<h4>Change successful!</h4>";
echo "<p>First name: " . $fname;
echo "<p>Last name: " . $lname;
echo "<p>Old email: " . $email;
echo "<p>New Email: " . $new_email;
}
else{
echo "<h4>Change Not Successful. There is no such existing records.</h4>";
echo "<p>Old email: " . $email;
}
}
//$backtrace = debug_backtrace();
//print_r($backtrace);
$stmt->close();
}
$conn->close();
}
saveMemberToDB();
?>
<?php include_once "include/footer.inc.php"; ?>
</body>
</html>