Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sessions. HTTP IS A STATELESS PROTOCOL Reminder Stateless Protocol (XKCD)

Similar presentations


Presentation on theme: "Sessions. HTTP IS A STATELESS PROTOCOL Reminder Stateless Protocol (XKCD)"— Presentation transcript:

1 Sessions

2 HTTP IS A STATELESS PROTOCOL Reminder

3 Stateless Protocol (XKCD)

4 Stateless Protocol (Technical)

5 SESSIONS The solution

6 session_start() Creates ssesion ID if none present in request Uses session ID, if present in the request Lets you use $_SESSION http://www.php.net/manual/en/function.sess ion-start.php http://www.php.net/manual/en/function.sess ion-start.php

7 How is my session associated with my request Each request sends a session id in one of two ways – Cookie – GET parameter (you want to avoid parameters whenever possible)

8 Where do session IDs come from? http://git.php.net/?p=php - src.git;a=blob;f=ext/sessio n/session.c;h=48b9d1157 744f58977eb2ac4a9759a ee0fc39324;hb=HEAD#l28 2 http://git.php.net/?p=php - src.git;a=blob;f=ext/sessio n/session.c;h=48b9d1157 744f58977eb2ac4a9759a ee0fc39324;hb=HEAD#l28 2 http://git.php.net/?p=php - src.git;a=blob;f=ext/hash/ hash_sha.c#l206 http://git.php.net/?p=php - src.git;a=blob;f=ext/hash/ hash_sha.c#l206 Advanced Technical Detail

9 Session IDs are numbers Hashes from random points in memory

10 What can I store in a session Serializable Data

11 What's this serialization thing all about: “It lets you take an object or group of objects, put them on a disk or send them through a wire or wireless transport mechanism, then later, perhaps on another computer, reverse the process: resurrect the original object(s). The basic mechanisms are to flatten object(s) into a one-dimensional stream of bits, and to turn that stream of bits back into the original object(s). Like the Transporter on Star Trek, it's all about taking something complicated and turning it into a flat sequence of 1s and 0s, then taking that sequence of 1s and 0s (possibly at another place, possibly at another time) and reconstructing the original complicated ‘something’.” http://www.parashift.com/c++-faq-lite/serialize- overview.html http://www.parashift.com/c++-faq-lite/serialize- overview.html

12 What’s serializable in PHP? Simple Data (2, “string”, [1, “a” => 2]) Objects

13 What’s not serializable in PHP? Resources – Network Sockets – File Handles – Database Connetions

14 How do I store things in $_SESSION? $_SESSION[“thing1”] = 1 $_SESSION[“my array”] = [1, 1, 2, 3, 5]

15 How do I retrieve things from $_SESSSION? $thing1 = $_SESSION[“thing1] $myArray = $_SESSION[“my array”]

16 How to destroy a session? session_destroy() http://www.php.net/manual/en/function.sess ion-destroy.php http://www.php.net/manual/en/function.sess ion-destroy.php

17 When do I destroy a session The most common reason to destroy a session is when a user logs out.

18 Where is the session store By default the PHP session backend uses files. http://www.php.net/manual/en/session.confi guration.php#ini.session.save-path http://www.php.net/manual/en/session.confi guration.php#ini.session.save-path http://www.php.net/manual/en/session.confi guration.php


Download ppt "Sessions. HTTP IS A STATELESS PROTOCOL Reminder Stateless Protocol (XKCD)"

Similar presentations


Ads by Google