Web Programming Step by Step, 2nd Edition

Lecture 13: Sessions

Reading: 14.3 - 14.4

Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Valid HTML5 Valid CSS

14.3: Sessions

How long does a cookie exist?

What is a session?

How sessions are established

session

Cookies vs. sessions

cookie monster

Sessions in PHP: session_start

session_start();

Accessing session data

$_SESSION["name"] = value;        # store session data
$variable = $_SESSION["name"];     # read session data
if (isset($_SESSION["name"])) {  # check for session data
if (isset($_SESSION["points"])) {
	$points = $_SESSION["points"];
	print("You've earned $points points.\n");
} else {
	$_SESSION["points"] = 0;  # default
}

Where is session data stored?

session cookie

Session timeout

Ending a session

session_destroy();

Common session bugs

Practice problem: Power Animal

power animal
power animal

Implementing user logins

user login

"Remember Me" feature

user login