Recall: Setting cookies in PHP

setcookie("name", "value");
...
$variable = $_COOKIE["name"];   # retrieve value of cookie later

PHP

Retrieving a cookie in PHP:

setcookie("username", "martay");
setcookie("age", 19);
...
if (isset($_COOKIE["username"])) {
	$username = $_COOKIE["username"];
	print("Welcome back, $username.\n");
}

PHP

Exercise 1: Longcat Count (by Phil Sheperd)

top
bottom
bottom

Use a cookie to count the number of times the user has viewed longcat.php. Display this count to the user on the page, e.g. "You have visited 7 time(s)." (sample solution) For added challenge:

Exercise 1 Solution

Exercise 2: Prize (by Michael Beswetherick)

prize.php is a travel site. We want roughly every 10th visitor to win a "free trip". (sample solution)

Exercise 2 Solution

Exercise 3: Birthdays (by Phil Sheperd)

cake What day of the month were you born on? Let's check the box for everyone's day in birthdays.php. The form submits to itself, but if you leave the page and come back, it forgets what boxes were checked.

Exercise 3 Solution