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.
session_start
session_start();
session_start
signifies your script wants a session with the user
session_start
:
$_SESSION
associative array$_SESSION
and retrieve it on future pages$_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
}
$_SESSION
associative array reads/stores all session dataisset
function to see whether a given value is in the sessionPHPSESSID
/tmp/sess_fcc17f071...
session_save_path
functionsession_set_save_handler
functionsession_cache_expire
functionsession_destroy
session_destroy();
session_destroy
ends your current session
session_start
again later, it sometimes reuses the same session ID/data you used before
session_destroy();
session_regenerate_id(TRUE); # flushes out session ID number
session_start();
session_start
doesn't just begin a session; it also reloads any existing session for this user. So it must be called in every page that uses your session data:
# the user has a session from a previous page print $_SESSION["name"]; # undefined session_start(); print $_SESSION["name"]; # joe
session_destroy(); session_regenerate_id(TRUE); session_start();
poweranimal.php
that chooses a random "power animal" for the user.