-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_user.php
280 lines (233 loc) · 7.5 KB
/
process_user.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
<?php
// we will only start the session with session_start() IF the session isn"t started yet //
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
?>
<?php
// including the conn.php to establish connection with database //
include "conn.php";
?>
<?php
/* Check the validation for the input fields by user */
/* Check if the email is existing while register */
if(isset($_POST['check_email'])){
$email = $_POST['email'];
$sql = "SELECT * FROM user WHERE user_email = '$email'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0){
echo "not_available";
}
else{
echo "available";
}
exit();
}
/* Check if the username is existing while register */
if(isset($_POST['check_username'])){
$username = $_POST['username'];
$sql = "SELECT * FROM user WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0){
echo "not_available";
}
else{
echo "available";
}
exit();
}
/* Check if the ic number is existing while register */
if(isset($_POST['check_icnumber'])){
$icnumber = $_POST['ic_number'];
$sql = "SELECT * FROM user WHERE user_icnumber = '$icnumber'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0){
echo "not_available";
}
else{
echo "available";
}
exit();
}
/* Insert new user into database */
if(isset($_POST['register_user'])){
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$icnumber = $_POST['icnum'];
$address = $_POST['address'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$stmt = $db -> prepare("INSERT INTO user(user_fname, user_lname, user_icnumber, user_address, user_email, user_username, user_password) VALUES (?,?,?,?,?,?,?) ");
$stmt ->bind_param("sssssss", $firstname, $lastname, $icnumber, $address, $email, $username, $password);
$stmt->execute();
$stmt->close();
mysqli_close($db);
echo "Register Successfully!";
exit();
}
/* Verify user and login */
if(isset($_POST['login_user'])){
$username = ($_POST['username']);
$password = md5($_POST['password']);
$stmt = $db->prepare("SELECT * FROM user WHERE user_username =? and user_password =?");
$stmt->bind_param("ss",$username, $password);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$_SESSION['username'] = $username;
$_SESSION['role'] = $row["user_role"];
$_SESSION['address'] = $row["user_address"];
$_SESSION['user_id'] = $row["user_id"];
$_SESSION['user_lname'] = $row["user_lname"];
$_SESSION['user_fname'] = $row["user_fname"];
$_SESSION['user_icnumber'] = $row["user_icnumber"];
$_SESSION['user_address'] = $row["user_address"];
$_SESSION['user_email'] = $row["user_email"];
}
$stmt->close();
mysqli_close($db);
if (isset($_SESSION['user_id'])) {
echo $_SESSION['role'];
} else {
echo "Incorrect username/password";
}
exit();
}
/* Show user details */
if(isset($_POST['get_detail'])){
$username = $_SESSION['username'];
$sql = "SELECT * FROM user WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
while ($rows = mysqli_fetch_array($result)){
?>
<ul style="font-size:15px;font-style: italic;color: grey;">
Name:<li style="list-style-type:none;font-size: 20px;color: black;font-style: normal;"><?php echo $rows['user_fname'] ?> <?php echo $rows['user_lname'] ?></li><br>
Username:<li style="list-style-type:none;font-size: 20px;color: black;font-style: normal;"><?php echo $rows['user_username'] ?></li><br>
IC Number:<li style="list-style-type:none;font-size: 20px;color: black;font-style: normal;"><?php echo $rows['user_icnumber'] ?></li><br>
Home Address:<li style="list-style-type:none;font-size: 20px;color: black;font-style: normal;"><?php echo $rows['user_address'] ?></li><br>
Email Address:<li style="list-style-type:none;font-size: 20px;color: black;font-style: normal;"><?php echo $rows['user_email'] ?></li><br>
</ul>
<?php
}
exit();
}
/* Logout user */
if(isset($_POST['logout_user'])){
$username = $_SESSION['username'];
echo "You have logged out! Thank you '$username'";
session_destroy();
}
/*Forget password*/
if(isset($_POST['forget_password'])){
$username = ($_POST['username']);
$email = ($_POST['email']);
$password = md5($_POST['password']);
$sql = "SELECT * FROM user WHERE user_username = '$username' AND user_email = '$email'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0){
$stmt = $db -> prepare("UPDATE user SET user_password=? WHERE user_username=?");
$stmt->bind_param("ss",$password,$username);
$stmt->execute();
$stmt->close();
mysqli_close($db);
echo "Request Accepted! Please try login with your new password.";
exit();
}
else{
echo "Not such user!";
}
}
/* Update User normal detail */
if(isset($_POST['update_user_normal'])){
$username = $_SESSION['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$icnumber = $_POST['icnumber'];
$address = $_POST['address'];
$stmt = $db -> prepare("UPDATE user SET user_fname=?,user_lname=?,user_icnumber=?,user_address=? WHERE user_username=?");
$stmt->bind_param("sssss",$firstname,$lastname,$icnumber,$address,$username);
$stmt->execute();
$stmt->close();
mysqli_close($db);
echo "Update Successfully!";
exit();
}
/* Update User email */
if(isset($_POST['update_user_email'])){
$username = $_SESSION['username'];
$email = $_POST['email'];
$stmt = $db -> prepare("UPDATE user SET user_email=? WHERE user_username=?");
$stmt->bind_param("ss",$email,$username);
$stmt->execute();
$stmt->close();
mysqli_close($db);
echo "Update Email Successfully!";
exit();
}
/* Update User password */
if(isset($_POST['update_user_password'])){
$username = $_SESSION['username'];
$password = md5($_POST['password']);
$stmt = $db -> prepare("UPDATE user SET user_password=? WHERE user_username=?");
$stmt->bind_param("ss",$password,$username);
$stmt->execute();
$stmt->close();
mysqli_close($db);
echo "Update Password Successfully!";
exit();
}
/* Check ic number while updating */
if(isset($_POST['check_icnumber_update'])){
$username = $_SESSION['username'];
$icnumber = $_POST['ic_number'];
$sql = "SELECT * FROM user WHERE user_icnumber = '$icnumber'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) == 0){
echo "available";
}else{
while ($rows = mysqli_fetch_array($result)){
if($username == $rows['user_username']){
echo "available";
}else{
echo "not_available";
}
}
}
exit();
}
/* Check password while updating */
if(isset($_POST['check_password_update'])){
$username = $_SESSION['username'];
$password = md5($_POST['password']);
$sql = "SELECT * FROM user WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
while ($rows = mysqli_fetch_array($result)){
if($password == $rows['user_password']){
echo "available";
}else{
echo "not_available";
}
}
exit();
}
/* Check email while updating */
if(isset($_POST['check_email_update'])){
$username = $_SESSION['username'];
$email = $_POST['email'];
$sql = "SELECT * FROM user WHERE user_email = '$email'";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) == 0){
echo "available";
}else{
while ($rows = mysqli_fetch_array($result)){
if($username == $rows['user_username']){
echo "available";
}else{
echo "not_available";
}
}
}
exit();
}
?>