-
Notifications
You must be signed in to change notification settings - Fork 0
/
+en_cookie.php
432 lines (431 loc) · 20 KB
/
+en_cookie.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!--
/*
Purpose: This file demonstrates the use of cookies in our website.
Project: Kirito website
Author: Vuong Khang Minh,Nghiem Tuan Linh, Nguyen Cuong Nhat, Dang Nguyen Duc Anh, Phan Huy Quang
Last Updated: 2023-4-7
*/
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Enhancement 2: Cookies</title>
<link rel="stylesheet" href="./styles/enhancement2_cookie.css" />
<link rel="stylesheet" href="./styles/footer_en.css">
</head>
<body>
<header>
<a class="back1" href="./enhancements2.php"> Back to main enhancement page.</a>
</header>
<article class="intro">
<h1>1st Enhancement: Cookies</h1>
<p>Scroll down for more info</p>
</article>
<hr />
<section class="page">
<h2>I. Definition and purpose</h2>
<p>
Cookies are small pieces of data that are stored on a user's
computer by the web browser while browsing a website. Cookies
were designed to be a reliable mechanism for websites to
remember stateful information (such as items added in the
shopping cart in an online store) or to record the user's
browsing activity (including clicking particular buttons,
logging in, or recording which pages were visited in the past).
They can also be used to remember arbitrary pieces of
information that the user previously entered into form fields
such as names, addresses, passwords, and credit card numbers.
</p>
<p>
In our case, cookies are used to store the user's name and email
address and password. This is done so that the user does not
have to enter their information every time they visit the
website.
</p>
<p class="page_tldr">
TL;DR: Cookies help websites remember user information. Our site
uses them to store user's name, email, and password.
</p>
</section>
<hr />
<section class="page">
<h2>II. Technical details</h2>
<p>
Let's take the snippets code from files "checksignin.php" and
"_checkuser.php" as an example. The first is the file
"checksignin.php":
</p>
<div class="code">
<div class="line">if ($row['user_pass'] == $user_pass) {</div>
<div class="line">
    if ($row['second_pw'] !== null) {
</div>
<div class="line">
        echo
"Redirecting...";
</div>
<div class="line">
        header("refresh:1;url=signinadmin.php");
</div>
<div class="line">
        $day = 7;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">passadmin</p>
", "
<p class="str">true</p>
",
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line">    } else {</div>
<div class="line">
        echo
"Redirecting...";
</div>
<div class="line">
        $day = 7;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">user</p>
", "
<p class="str">true</p>
",
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">name</p>
",
<p class="var space_line_left">$</p>
username,
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">pass</p>
",
<p class="var space_line_left">$</p>
user_pass,
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">un</p>
",
<p class="var space_line_left">$</p>
un,
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line highlight">
        
<p class="function">setcookie(</p>
"
<p class="str">email</p>
",
<p class="var space_line_left">$</p>
row
<p class="function">[</p>
'
<p class="str">email</p>
'
<p class="function">]</p>
,
<p class="function space_line_right space_line_left">
time()
</p>
<p class="operator space_line_right">+</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">60</p>
<p class="operator space_line_right">*</p>
<p class="num space_line_right">24</p>
<p class="operator space_line_right">*</p>
<p class="var">$</p>
day
<p class="function">)</p>
;
</div>
<div class="line">
        header("refresh:1;url=user.php");
</div>
<div class="line">    }</div>
<div class="line">}</div>
</div>
<p class="page_tldr">
In this code, the cookies are used to remember if the user is an
administrator or a regular user, and what their name, password,
username, email address and unique identifier are. The cookies
have an expiration time of seven days, which means that they
will be deleted from the browser after that period. This way,
the user does not have to enter their credentials every time
they visit the web site, as long as they use the same browser
within seven days.
</p>
</section>
<hr />
<section class="page">
<h2>II. Technical details</h2>
<p>The second is the file "_checkuser.php":</p>
<div class="code">
<div class="line">function idCorrectness() {</div>
<div class="line">
    require_once "settings.php";
</div>
<div class="line">
    $conn = @mysqli_connect($host,
$user, $pwd, $sql_db);
</div>
<div class="line">
    if (!$conn) { echo
"<p>Database connection failure.</p>"; exit(); }
</div>
<div class="line">
    $sql_table = "assign2_users";
</div>
<div class="line highlight">
    
<p class="var">$</p>
username
<p class="var space_line_left">= $</p>
_COOKIE
<p class="operator">[</p>
'
<p class="str">name</p>
'
<p class="operator">]</p>
<p class="var space_line_right space_line_left">??</p>
<p class="var">null;</p>
</div>
<div class="line highlight">
    
<p class="var">$</p>
userpass
<p class="var space_line_left">= $</p>
_COOKIE
<p class="operator">[</p>
'
<p class="str">pass</p>
'
<p class="operator">]</p>
<p class="var space_line_right space_line_left">??</p>
<p class="var">null;</p>
</div>
<div class="line">
    if ($username == null || $userpass
== null) {
</div>
<div class="line">
        return
false;
</div>
<div class="line">    }</div>
<div class="line">
    $sql_command = "SELECT username,
user_pass FROM $sql_table WHERE username = '$username' AND
user_pass = '$userpass'";
</div>
<div class="line">
    $result = mysqli_query($conn,
$sql_command);
</div>
<div class="line">
    if (mysqli_num_rows($result) > 0) {
</div>
<div class="line">
        return true;
</div>
<div class="line">    } else {</div>
<div class="line">
        return true;
</div>
<div class="line">    }</div>
<div class="line">}</div>
</div>
<p class="page_tldr">
This code snippet creates a function that verifies the user's
identity and role by comparing the information stored in their
browser and the information stored in the web server. The
function returns a boolean value, which is a data type that can
only have two possible values: true or false. The function
returns true if the user’s name and password match the ones in
the database, and false if they do not match or if the cookies
do not exist.
</p>
</section>
<hr />
<section class="page">
<h2>II. Technical details (bonus)</h2>
<p>
We will have this snippet to remove cookie of user in
'logout.php' file
</p>
<div class="code">
<div class="line">foreach ($_COOKIE as $key => $value) {</div>
<div class="line">
    setcookie($key, "", time() - 3600);
</div>
<div class="line">}</div>
</div>
<p class="page_tldr">
To sum up, this php code is using a foreach loop to iterate over
the $_COOKIE array, which contains all the cookies that are set
on the client’s browser. For each cookie, it calls the setcookie
function with an empty value and a negative expiration time,
which effectively deletes the cookie. This code can be used to
clear all the cookies from the client’s browser.
</p>
<p class="small_text">
Nevertheless, upon executing this code, we anticipate that all
cookies will be erased (except those set by third parties).
Contrary to our expectation, the “admin” cookie was not deleted.
Upon further investigation, we discovered that the php parser in
xampp might have encountered an issue (presumably?) and
overlooked the admin cookie. We kindly request you to verify
that this is a software bug.
</p>
</section>
<hr />
<section class="page">
<h2>III. Testing / How to test</h2>
<p>
One possible method of evaluating this enhancement is to eliminate all
the cookies associated with this website. This action would
prevent the access to the pages designated for managers and
users. Instead of opening these pages, a 403 error would appear
and the browser would redirect to other pages within a short
time span. This method would demonstrate the functionality of
the cookies and their role in enabling the access to different
pages. An alternative method is to log out from the website
(assuming prior registration), verify that the cookies have been
removed from the browser settings, and then log in again,
confirming that the cookies have been reinstated. This method
would also show how the cookies are related to the user
authentication and authorization process. Both methods would
provide useful information about our use of cookies and its features.
</p>
<a class="go_back_enhancement" href="./enhancements2.php">[Go back to main enhancement page]</a>
</section>
<hr />
<footer>
<div class="col">
<div class="headr">Kirito</div>
<div class="body">
<p>A full service branding games for gamers</p>
<p>Term of Service</p>
<p>Privacy Policy</p>
<p>© 2021 Kirito. All rights reserved.</p>
</div>
</div>
<div class="col">
<div class="headr">Get in touch</div>
<div class="body">
<p>Monday - Friday</p>
<p>9:00 AM - 5:00 PM</p>
<p>Contact</p>
<p>Facebook</p>
<p>Instagram</p>
</div>
</div>
<div class="col">
<div class="headr">Join our Mailing List</div>
<div class="body">
<form class="bodyform" action="./enquire.php">
<input type="text" name="first_name" placeholder="First Name">
<input type="email" name="email" placeholder="Email">
<input type="submit" value="Submit">
</form>
</div>
</div>
<div class="col">
<div class="headr">Explore</div>
<div class="body">
<a href="./index.php">Home</a>
<a href="./product.php">Product</a>
<a href="./payment.php">Enquire / Payment</a>
<a href="./about.php">About</a>
<a href="./enhancements.php">Enhancements</a>
<a href="./enhancements2.php">(New!!) Enhancements 2</a>
</div>
</div>
</footer>
</body>
</html>