And Sessions....
Exploration session
If you were pre approved for the alternate ES and still want credit, you have until tomorrow
Exploration Session 4 with Shuowei Li on Thursday 5/31 5:30pm in MGH 241.
CP 7 showcase is up
HW 6 due today (resources.zip has 2 more books if you want to try)
In the home stretch! CP 9 (was 10) announced, due Tuesday 5/29!
CAUSE Catalyst survey in email
HTTP is a stateless protocol; it simply allows a browser to request a single document from a web server
Once the document has been sent to the client, the server does not keep track of any information about what was sent (other than maybe in a log file of the transaction).
Sites like amazon.com seem to "know who I am." How do they do this? How does a client uniquely identify itself to a server, and how does the server provide specific content to each client?
Today we'll learn some technologies that are used to store "state" that can then be sent between clients and servers.
document.cookie
setcookie()
$_COOKIE
document.cookie = cookieString;
cookieString
consists of 3 semicolon separated parts (the second two are optional):
"lastItemBought=apples"
"expires=Thu, 23 May 2018 12:00:00 UTC"
"path=/"
document.cookie = "lastItemBought=apples; " +
"expires=Thu, 23 May 2018 12:00:00 UTC; " +
"path=/";
JavaScript (example)
let cookies = document.cookie;
"lastItemBought=apples; numberSiteVisitsToday=57"
To delete a cookie you need to set it's expiration time to be before now
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
JavaScript (example)
setcookie("name", "value", expiration);
PHP (template)
$expireTime = time() + 60*60*24*7; # 1 week from now
setcookie("CouponNumber", "389752", $expireTime);
setcookie("CouponValue", "100.00", $expireTime);
PHP (example)
$variable = $_COOKIE["name"]; # retrieve value of the cookie
PHP (example)
if (isset($_COOKIE["username"])) {
$username = $_COOKIE["username"];
print("Welcome back, $username.\n");
} else {
print("Never heard of you.\n");
}
print("All cookies received:\n");
print_r($_COOKIE);
PHP
setcookie("name", FALSE);
PHP (example)
setcookie("CouponNumber", FALSE);
PHP
session: an abstract concept to represent a series of HTTP requests and responses between a specific Web browser and server
HTTP doesn't support the notion of a session, but PHP does
session_start();
PHP
session_start
signifies your script wants a session with the user
session_start
do?
$_SESSION
associative array$_SESSION
and retrieve it on future pages
session_start();
$_SESSION["name"] = value; # store session data
$variable = $_SESSION["name"]; # read session data
if (isset($_SESSION["name"])) { # check for session data
PHP (template)
session_start();
if (isset($_SESSION["points"])) {
$points = $_SESSION["points"];
print("You've earned $points points.\n");
} else {
$_SESSION["points"] = 0; # default
}
PHP
isset
function to see whether a given value is in the session