forked from bramus/ws1-sws-course-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.persistence.html
1037 lines (884 loc) · 30.8 KB
/
09.persistence.html
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Webscripting1 — Serverside Webscripting — 09.persistence</title>
<meta name="description" content="Webscripting1 — Serverside Webscripting — 09.persistence">
<meta name="author" content="Bram(us) Van Damme - ikdoeict.be">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css" media="screen">
<link rel="stylesheet" href="css/print.css" media="print">
<link rel="stylesheet" href="lib/zenburn.css">
<style>
.columns .column {
float: left;
list-style: none;
margin: 0 0 12px 0;
padding: 0;
}
.column-12 {
width: 50%;
text-align: center;
}
code {
color: gainsboro;
}
li > code, li em > code, li del > code, li ins > code, p > code {
background: #3F3F3F;
padding: 2px 4px;
box-shadow: 0px 0px 6px rgba(0,0,0,0.3);
font-size: 80%;
}
del code, code.nok {
color: #C55;
}
ins code, code.ok {
color: #5C5;
}
strong {
color: #553d00;
background: transparent url('assets/02/strong.png') no-repeat 50% 50%;
font-size: 80%;
padding: 0 4px;
font-family: palatino, times;
font-weight: 300;
font-style: italic;
}
#reveal section img.noborder {
background: transparent;
border: 0;
-webkit-box-shadow: none;
-moz-box-shadow: none;
-ms-box-shadow: none;
-o-box-shadow: none;
box-shadow: none;"
}
#reveal .hastooltip {
position: relative;
}
#reveal .hastooltip .tooltip {
position: absolute;
bottom: 5px;
right: 5px;
background-image: url('assets/comments.png');
background-position: bottom right;
background-repeat: no-repeat;
width: 60px;
height: 60px;
z-index: 110;
}
#reveal .tooltip div {
display: none;
position: absolute;
bottom: 30px;
right: 40px;
font-size: 50%;
line-height: 1.48;
min-width: 420px;
opacity: 0.9;
border: 2px solid #ccc;
padding: 20px;
background-color: #ffc;
background-image: -webkit-linear-gradient(rgba(255,255,255,.5), rgba(255,255,180,0));
background-image: -moz-linear-gradient(rgba(255,255,255,.5), rgba(255,255,180,0));
background-image: -ms-linear-gradient(rgba(255,255,255,.5), rgba(255,255,180,0));
background-image: -o-linear-gradient(rgba(255,255,255,.5), rgba(255,255,180,0));
background-image: linear-gradient(rgba(255,255,255,.5), rgba(255,255,180,0));
border-radius: 4px;
box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
text-shadow: 0 1px 0 rgba(255,255,255,.4);
color: #333;
}
#reveal .tooltip:hover div {
display: block;
}
#reveal .tooltip ul {
margin: 5px 5px 5px 15px;
}
#reveal .tooltip ul li {
margin-left: 10px;
}
#reveal .tooltip div *:last-child {
margin-bottom: 0;
}
@keyframes pizzapalace-cycle {
0% { background-position: 0 0; }
100% { background-position: -2290px 0; }
}
#pizzapalace-strip {
display: block;
width: 572px;
height: 526px;
background: transparent url(assets/09/pizzapalace-strip.png) no-repeat 0 0 !important;
background-size: 2308px 526px !important;
transform-origin: center top;
transform: scale(0.6);
top: -320px;
margin: 0 auto 0;
transition: all 0.25s;
}
#pizzapalace-strip:hover {
transform: scale(1);
top: -200px;
animation: pizzapalace-cycle 12s steps(4) infinite;
}
@keyframes colormysite-cycle {
0% { background-position: 0 0; }
100% { background-position: -1112px 0; }
}
#colormysite-strip {
display: block;
width: 556px;
height: 480px;
background: transparent url(assets/09/colormysite-strip.png) no-repeat 0 0 !important;
background-size: 1112px 480px !important;
transform-origin: center top;
transform: scale(0.6);
top: -120px;
margin: 0 auto 0;
transition: all 0.25s;
}
#colormysite-strip:hover {
transform: scale(1);
animation: colormysite-cycle 4s steps(2) infinite;
}
@keyframes login-cycle {
0% { background-position: 0 0; }
100% { background-position: -3395px 0; }
}
#login-strip {
display: block;
width: 580px;
height: 449px;
background: transparent url(assets/09/login-strip.png) no-repeat 0 0 !important;
background-size: 3395px 449px !important;
transform-origin: center top;
transform: scale(0.6);
top: -220px;
margin: 0 auto 0;
transition: all 0.25s;
}
#login-strip:hover {
top: -120px;
transform: scale(1);
animation: login-cycle 12s steps(6) infinite;
}
</style>
</head>
<body>
<div id="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- 0 : Title -->
<section>
<section>
<h3 class="inverted">Serverside Webscripting <small>[JLW322]</small></h3>
<h1>09.persistence</h1>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
<script>
// Delicously hacky. Look away.
if( navigator.userAgent.match( /(iPhone|iPad|iPod|Android)/i ) )
document.write( '<p style="color: rgba(0,0,0,0.3); text-shadow: none;">('+'Tap to navigate'+')</p>' );
</script>
</section>
</section>
<section>
<section>
<h2>Persistence</h2>
</section>
<section>
<h2>Persistence?</h2>
<ul>
<li class="fragment">
HTTP is stateless
<ul class="fragment">
<li>HTTP does not provide a mechanism to keep or maintain information about other requests that might have happened</li>
</ul>
</li>
<li class="fragment">
How can we keep information between two requests?
<ul class="fragment">
<li>How can we persist data between requests?</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Example: Pizza Palace</h2>
<ul>
<li class="fragment">
How can we let the last page of this example site know what choices you made on the pages before?
<a href="assets/09/examples/1.pizzapalace/v0" id="pizzapalace-strip"></a>
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Persistence using the querystring</h2>
</section>
<section>
<h2>Persistence using the querystring</h2>
<ul>
<li class="fragment">Collect all selected options, and attach them to the querystring of the <code>action</code> of the form</li>
<li class="fragment">On the final page, all data can be found in <strong>a combination</strong> of <code>$_GET</code> and <code>$_POST</code></li>
<li class="fragment">
Beware: only works with <code><form method="post"></code>
<ul>
<li class="fragment">Reason: when the method is <code>get</code>, the browser discards the already attached parameters from <code>action</code> and then adds all the fields of the form to it</li>
</ul>
</code>
</ul>
</section>
<section>
<h2>Code</h2>
<ul>
<li class="fragment">
Let's take a peek
<div class="hastooltip">
<div style="text-align: center">
<figure>
<img src="assets/09/pizzapalace-code-querystring.png" class="noborder" width="710" alt="" title="" />
</figure>
<p style="font-size: 75%; margin-top: -1em;"><code>/assets/09/examples/1.pizzapalace/v1-querystring</code> <a href="assets/09/examples/1.pizzapalace/v1-querystring">→</a></p>
</div>
<div class="tooltip">
<div>
<p>On each page, extract the form parameters from <code>$_POST</code>, and add it to the <code>action</code> of the form. Remember to <code>urlencode()</code> them!</p>
<p>On the following pages, extract the values to persist from <code>$_GET</code> and re-add them to the form</p>
<p>On the final page, all parameters can be found in a combination of <code>$_GET</code> and <code>$_POST</code></p>
</div>
</div>
</div>
</li>
</ul>
</section>
<section>
<h2>Afterthoughts</h2>
<ul>
<li class="fragment">Querystring = Cumbersome
<ul>
<li class="fragment">
Per piece of data, you need to add an extra parameter to the querystring
<ul>
<li class="fragment">Even if you don't need that data immediately</li>
</ul>
</li>
<li class="fragment">The client needs to re-send all data for each request</li>
</ul>
</li>
<li class="fragment">
Querystring = Limited
<ul>
<li class="fragment">Querystrings are limited in length (at about 2000 chars some browsers will start failing)!</li>
</ul>
</li>
<li class="fragment">
Querystring = Insecure
<ul>
<li class="fragment">Adding a <code>?loggedin=true</code> parameter to the querystring is not secure</li>
<li class="fragment">Params may be spoofed</li>
<li class="fragment">URLs my be passed on</li>
</ul>
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Persistence using hidden fields</h2>
</section>
<section>
<h2>Persistence using hidden fields</h2>
<ul>
<li class="fragment">Collect all selected options, and add them to the form as hidden fields</li>
<li class="fragment">On the final page, all data can be found in <strong>either</strong> <code>$_GET</code> or <code>$_POST</code>, depending on the <code>method</code> used</li>
</ul>
</section>
<section>
<h2>Code</h2>
<ul>
<li class="fragment">
Let's take a peek
<div class="hastooltip">
<div style="text-align: center">
<figure>
<img src="assets/09/pizzapalace-code-hiddenfields.png" class="noborder" width="710" alt="" title="" />
</figure>
<p style="font-size: 75%; margin-top: -1em;"><code>/assets/09/examples/1.pizzapalace/v2-hiddenfields</code> <a href="assets/09/examples/1.pizzapalace/v2-hiddenfields">→</a></p>
</div>
<div class="tooltip">
<div>
<p>On each page, extract the form parameters from <code>$_GET</code> or <code>$_POST</code>, and add it as a hidden field in the form.</p>
<p>On the following pages, extract the values to persist from <code>$_GET</code> or <code>$_POST</code> and re-add them to the form</p>
<p>On the final page, all parameters can be found in either <code>$_GET</code> or <code>$_POST</code>, depending on the form <code>method</code> used.</p>
</div>
</div>
</div>
</li>
</ul>
</section>
<section>
<h2>Afterthoughts</h2>
<ul>
<li class="fragment">Hidden Fields = Cumbersome
<ul>
<li class="fragment">
Per piece of data, you need to add an extra parameter to the querystring
<ul>
<li class="fragment">Even if you don't need that data immediately</li>
</ul>
</li>
<li class="fragment">The client needs to re-send all data for each request</li>
</ul>
</li>
<li class="fragment">
Hidden Fields = Limited
<ul>
<li class="fragment">When using <code>$_GET</code>: limit of querystring</li>
<li class="fragment">
When using <code>$_POST</code>: limit set by server
<pre class="bigger"><code class="language-php">echo ini_get('post_max_size');</code></pre>
</li>
</ul>
</li>
<li class="fragment">
Hidden Fields = Insecure
<ul>
<li class="fragment">Adding a <code>loggedin=true</code> hidden field is not secure</li>
<li class="fragment">Params may be spoofed</li>
<li class="fragment">URLs my be passed on</li>
</ul>
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Persistence using cookies</h2>
</section>
<section>
<h2>Cookies</h2>
<ul>
<li class="fragment">Store data on the client-side using <code>name=value</code> pairs</li>
<li class="fragment">
Cookie usage not unlimited; Minimum requirements set via <a href="http://www.ietf.org/rfc/rfc2109.txt">RFC 2109</a> <em>(item 6.3, page 15)</em>
<ul>
<li class="fragment">
Allow a minimum of 4kb to be stored in a cookie
<ul>
<li>All browsers: 4kb max</li>
</ul>
</li>
<li class="fragment">
Allow at least 20 cookies per domain
<ul>
<li>Firefox & IE: 50 max</li>
<li>Opera: 30 max</li>
<li>Safari: unlimited</li>
</ul>
</li>
<li class="fragment">
Allow at least 300 cookies in total
<ul>
<li>Firefox: 1000 max</li>
<li>IE & Opera: unknown</li>
</ul>
</li>
</ul>
</li>
<li class="fragment">The limits don't form a problem most of the time</li>
</ul>
</section>
<section>
<h2>Client-server traffic</h2>
<ul>
<li class="fragment">
Cookies are stored client-side.
<ul>
<li class="fragment">When visiting a website, the client will send all (valid) cookies to the server via a header in the request head</li>
</ul>
</li>
<li class="fragment">
The server can instruct a client to set a cookie.
<ul>
<li class="fragment">The instruction is sent via a header in the server response head</li>
<li class="fragment">Only after (!) the entire response has been received will the client create the cookie</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Cookie properties (1)</h2>
<ul>
<li class="fragment">
Properties/Fields
<ul>
<li><code>name</code></li>
<li><code>value</code></li>
<li><code>expires</code></li>
<li><code>path</code></li>
<li><code>domain</code></li>
<li><code>secure</code></li>
</ul>
</li>
<li class="fragment">
<code>name</code> and <code>value</code>
<ul>
<li class="fragment">Name of the cookie and its value</li>
<li class="fragment">Minimum required properties of a cookie</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Cookie properties (2)</h2>
<ul>
<li class="fragment">
<code>expires</code>
<ul>
<li class="fragment">
Time when a cookie should expire
<ul>
<li>Default: current time</li>
</ul>
</li>
<li class="fragment">Expire a week from now: <code>time() + 60*60*24*7</code></li>
<li class="fragment">
Delete a cookie by setting a time in the past <code>time() - 1</code>
<ul>
<li>Best is to take a point in time way back, to overcome time differences between the server and client: <code>time() - 60*60*24*7</code></li>
</ul>
</li>
</ul>
</li>
<li class="fragment">
<code>path</code>
<ul>
<li class="fragment">
Path on the domain on which the cookie is valid
<ul>
<li>Default: current path</li>
</ul>
</li>
<li class="fragment">
Cookie created on <code>/admin</code> can't be read from <code>/</code>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Cookie properties (3)</h2>
<ul>
<li class="fragment">
<code>domain</code>
<ul>
<li class="fragment">
Domain on which the cookie is valid
<ul>
<li>Default: current domain</li>
</ul>
</li>
<li class="fragment">
If <code>www.ikdoeict.be</code> instructs a cookie to be created for <code>ikdoeict.be</code>, then that cookie can also be read from <code>student.ikdoeict.be</code>
</li>
</ul>
</li>
<li class="fragment">
<code>secure</code>
<ul>
<li class="fragment">
Indicates if a cookie may only be transmitted from the client to the server when running over a secure connection
<ul>
<li>Default: <code>false</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Creating Cookies in PHP</h2>
<ul>
<li class="fragment">
Use a function named <code>setcookie()</code>
<ul>
<li class="fragment">Via the response header, an instruction is sent to the client to create a cookie</li>
<li class="fragment">
Function Parameters: the properties as described before
<ul>
<li>Only <code>name</code> and <code>value</code> are mandatory</li>
</ul>
</li>
<li class="fragment">
Example
<pre class="bigger"><code class="language-php">setcookie('color', $theValue, time() + 24*60*60*7);</code></pre>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Reading Cookies in PHP</h2>
<ul>
<li class="fragment">
Via a request header, all (*) cookies are automatically sent to the server when making a request.
</li>
<li class="fragment">
PHP <em>automagically</em> populates a global associative array <code>$_COOKIE</code>
</li>
<ul>
<li class="fragment">Like <code>$_GET</code> and <code>$_POST</code>, but for cookies</li>
<li class="fragment">
Example
<pre class="bigger"><code class="language-php">$color = (string) isset($_COOKIE['color']) ? $_COOKIE['color'] : '#FFFFFF';</code></pre>
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Example: Color my site</h2>
<ul>
<li class="fragment">
Change the background color of the page and store it in a cookie
<a href="assets/09/examples/2.sitecolor/v0" id="colormysite-strip"></a>
</li>
</ul>
</section>
<section>
<h2>Code</h2>
<ul>
<li class="fragment">
Let's take a peek
<div class="hastooltip">
<div style="text-align: center">
<figure>
<img src="assets/09/colormysite-code-v1.png" class="noborder" width="710" alt="" title="" />
</figure>
<p style="font-size: 75%; margin-top: -1em;"><code>/assets/09/examples/2.sitecolor/v1-delay</code> <a href="assets/09/examples/2.sitecolor/v1-delay">→</a></p>
</div>
<div class="tooltip">
<div>
<p>Although the code looks OK, someting odd is happening: the color changes indeed, but with a one-page delay. Refreshing the page in the browser seems to fix this too</p>
<p>The problem relies in the fact that <code>setcookie()</code> doesn't create a cookie, but sends an instruction to create one. At the time the page is being rendered, the cookie hasn't change yet (as the browser creates the cookie when the HTML's already been rendered)</p>
</div>
</div>
</div>
</li>
</ul>
</section>
<section>
<h2>Code, revisited</h2>
<ul>
<li class="fragment">
Let's take a peek
<div class="hastooltip">
<div style="text-align: center">
<figure>
<img src="assets/09/colormysite-code-v2.png" class="noborder" width="710" alt="" title="" />
</figure>
<p style="font-size: 75%; margin-top: -1em;"><code>/assets/09/examples/2.sitecolor/v2-forcedrefresh</code> <a href="assets/09/examples/2.sitecolor/v2-forcedrefresh">→</a></p>
</div>
<div class="tooltip">
<div>
<p>By using a <code>header('location: ...');</code> we can enforce a refresh from within our PHP code. That way, when the page reloads, the cookie will be present.</p>
</div>
</div>
</div>
</li>
</ul>
</section>
<section>
<h2>Afterthoughts</h2>
<ul>
<li class="fragment">
Ideal for storing data that needs to be saved for a longer time / in between sessions
</li>
<li class="fragment">
Cookies = Uncertain
<ul>
<li class="fragment">
Cookies may be manually deleted by the client
</li>
<li class="fragment">
Cookies may be disabled (browser settting)
</li>
<li class="fragment">
Cookies may overwrite eachother (FIFO)
</li>
</ul>
</li>
<li class="fragment">
Cookies = Insecure
<ul>
<li class="fragment">
May be changed/read externally
</li>
<li class="fragment">
May be stolen via XSS
</li>
</ul>
</li>
<li class="fragment">
Only to be used for storing non-critical data such as a color preference for example
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Persistence using sessions</h2>
</section>
<section>
<h2>Sessions</h2>
<ul>
<li class="fragment">
Session = period of time in which a user interacts with a website
</li>
<li class="fragment">
Session starts with first request to a site and stops when you shut down the browser, or after a timeout
<ul>
<li>Timeout is configurable on the server</li>
</ul>
</li>
<li class="fragment">
A session is identified by a <strong>session id</strong>
<ul>
<li>
The client needs to send the identifier with each request so that the server knows which session it is about
<ul>
<li>May be sent using the querystring, a hidden field or a cookie</li>
<li>If you forget sending it once, a new session id will be generated</li>
</ul>
</li>
</ul>
</li>
<li class="fragment">
Session variables are stored serverside, linked to the session id
</li>
</ul>
</section>
<section>
<h2>Sessions in PHP</h2>
<ul>
<li>
In PHP
<ul>
<li class="fragment">
The session id is stored in variable named <code>PHPSESSID</code>
<ul>
<li>Name of the variable is configurable</li>
</ul>
</li>
<li class="fragment">PHP provides you some functions to create, manipulate, and destroy sessions</li>
<li class="fragment">PHP fetches the variables linked to the session id and populates an associative array <code>$_SESSION</code> with them</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Sessions, practical (1)</h2>
<ul>
<li>
Starting / continuing a session
<pre class="bigger"><code class="language-php">session_start();</code></pre>
<ul>
<li class="fragment">
Searches for the <code>PHPSESSID</code> in <code>$_COOKIE</code>, <code>$_GET</code>, or <code>$_POST</code>
<ul>
<li class="fragment">If not found, a (new) session id is generated</li>
<li class="fragment">If found, all linked vars are fetched and <code>$_SESSION</code> is populated</li>
</ul>
</li>
<li class="fragment">
Best is to call this function on each page, even if that page doesn't use the session data
</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Sessions, practical (2)</h2>
<ul>
<li class="fragment">
Storing a var in the session
<pre class="bigger"><code class="language-php" data-overlay-example="assets/09/examples/3.stranger/somepage.php">session_start();
$_SESSION['name'] = 'Bramus!';</code></pre>
</li>
<li class="fragment">
Fetching a var from the session
<pre class="bigger"><code class="language-php" data-overlay-example="assets/09/examples/3.stranger/someotherpage.php">session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : 'stranger';</code></pre>
</li>
<li class="fragment">
Erasing a var from the session
<pre class="bigger"><code class="language-php" data-overlay-example="assets/09/examples/3.stranger/unset.php">session_start();
unset($_SESSION['name']);</code></pre>
</li>
<li class="fragment">
Erasing an entire session
<pre class="bigger"><code class="language-php" data-overlay-example="assets/09/examples/3.stranger/destroy.php">session_start();
// Best practice: unset all session vars before stopping the session
foreach ($_SESSION as $key => $value) unset($_SESSION[$key]);
session_destroy();</code></pre>
</li>
</ul>
</section>
<section>
<h2>Passing the PHPSESSID</h2>
<ul>
<li class="fragment">
Session id <strong>must</strong> be sent with each request
<ul>
<li>hidden fields (manual)</li>
<li>query string (manual)</li>
<li>cookies (automatic)</li>
</ul>
</li>
<li class="fragment">
Example using querystring
<pre class="bigger"><code class="language-php">session_start();
echo '<a href="nextpage.php?PHPSESSID=' . session_id() . '" title="to next page">to next page</a>';</code></pre>
</li>
<li class="fragment">
Cookies do it automatically, based upon a <code>php.ini</code> setting
<pre class="bigger"><code>session.use_cookies = 1</code></pre>
<ul>
<li class="fragment">Enabled by default</li>
<li class="fragment">Easy, as you as a developer don't need to do any extra work</li>
<li class="fragment">Disadvantage: inherits all disadvantages that cookies have</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Some php.ini settings</h2>
<pre class="bigger"><code class="language-php">// path where sessions are saved on the server
session.save_path = /tmp
// name of the session id variable
session.name = PHPSESSID
// use cookies for storing the session id?
session.use_cookies = 1
// exipiry time of the cookie. Default value: when the browser closes
session.cookie_lifetime = 0
// expiry time of session variables
session.gc_maxlifetime = 1440</code></pre>
</section>
<section>
<h2>Example: Authentication</h2>
<ul>
<li class="fragment">
A login and logout page, along with three content pages, showing content based upon the logged in state.
<a href="assets/09/examples/4.login/v0" id="login-strip"></a>
</li>
</ul>
</section>
<section>
<h2>Code</h2>
<ul>
<li class="fragment">
Let's take a peek
<div class="hastooltip">
<div style="text-align: center">
<figure>
<img src="assets/09/login-code.png" class="noborder" width="710" alt="" title="" />
</figure>
<p style="font-size: 75%; margin-top: -1em;"><code>/assets/09/examples/4.login/v1</code> <a href="assets/09/examples/4.login/v1">→</a></p>
</div>
<div class="tooltip">
<div>
<p>The login page is very simple for this proof-of-concept: any login where the username equals the password is accepted</p>
<p>If the login validates, a <code>loggedIn</code> variable is stored in the session, along with the <code>name</code></p>
<p>The content pages use a simple <code>if</code> to display the <code>$_SESSION['name']</code> or a request to log in</p>
<p>The logout page simply destroys the session and redirects to the index</p>
</div>
</div>
</div>
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Summary</h2>
</section>
<section>
<h2>Summary</h2>
<ul>
<li class="fragment">
4 ways to persist data between requests
<ol>
<li>Querystring</li>
<li>Hidden Fields</li>
<li>Cookies</li>
<li>Sessions</li>
</ol>
</li>
<li class="fragment">
Querystring and Hidden fields not practical when persisting lots of data. Okay for storing small amounts of (non-critical) data.
</li>
<li class="fragment">Cookies are okay to storing (non-critical) data for a longer period of time</li>
<li class="fragment">Sessions suggested above all</li>
</ul>
</section>
</section>
<!-- The END -->
<section>
<section>
<h2>Questions?</h2>
<footer>
<em><a href="http://www.ikdoeict.be/">ikdoeict.be</a> — <a href="mailto:[email protected]">[email protected]</a></em>
</footer>
</section>
</section>
<!-- Summary -->
<section id="summary">
<section>
<h2>Code summary</h2>
<p style="margin-top: 5.5em;"><em>A code-only summary of this chapter available is at <a href="09.persistence.summary.html">09.persistence.summary.html</a></em></p>
<footer>
<em>Note: not all information from these slides can be found in this summary!</em>
</footer>
</section>
</section>
<!-- Sources -->
<section id="sources">
<section>
<h2>Sources</h2>
<ul>
<li><a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQs: What is the maximum length of a URL?</a></li>
</ul>