180 likes | 398 Views
Sessions. Reminder. HTTP is a Stateless Protocol. Stateless Protocol (XKCD). Stateless Protocol (Technical). The solution. Sessions. session_start (). Creates ssesion ID if none present in request Uses session ID, if present in the request Lets you use $_SESSION
E N D
Reminder HTTP is a Stateless Protocol
The solution Sessions
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.session-start.php
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)
Where do session IDs come from? • http://git.php.net/?p=php-src.git;a=blob;f=ext/session/session.c;h=48b9d1157744f58977eb2ac4a9759aee0fc39324;hb=HEAD#l282 • http://git.php.net/?p=php-src.git;a=blob;f=ext/hash/hash_sha.c#l206 Advanced Technical Detail
Session IDs are numbers • Hashes from random points in memory
What can I store in a session Serializable Data
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
What’s serializable in PHP? • Simple Data (2, “string”, [1, “a” => 2]) • Objects
What’s not serializable in PHP? • Resources • Network Sockets • File Handles • Database Connetions
How do I store things in $_SESSION? • $_SESSION[“thing1”] = 1 • $_SESSION[“my array”] = [1, 1, 2, 3, 5]
How do I retrieve things from $_SESSSION? • $thing1 = $_SESSION[“thing1] • $myArray = $_SESSION[“my array”]
How to destroy a session? • session_destroy() • http://www.php.net/manual/en/function.session-destroy.php
When do I destroy a session • The most common reason to destroy a session is when a user logs out.
Where is the session store • By default the PHP session backend uses files. • http://www.php.net/manual/en/session.configuration.php#ini.session.save-path • http://www.php.net/manual/en/session.configuration.php