1. Sessions
session_start() function
session_register(‘variable’) function
$_SESSION[‘variable’]
2. Before you begin, check php.ini
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
session.save_path = "c:/wamp/tmp“
For Windows, modify session.save_path so that a session
file can be written to a directory that exists. In the case of
WAMP, it already exists.
4. For cs346.cs.uwosh.edu
/var/lib/php5 already exists and is empty
huen@CS346:/var/lib/php5$ sudo pwd
/var/lib/php5
huen@CS346:/var/lib/php5$ sudo ls -l
total 0
huen@CS346:/var/lib/php5$
5. What is a session?
• Time perspective:
• A session is the length of time during which a user
visits a site
• Programming perspective:
– A session is a big blob that can hold all sorts of variables
and values
– The blob has an identification string e.g.
sess_fc48gdbuckuj9oe3ef74i00b01
– The identification string is sent to the user when a session
is initiated in a cookie called PHPSESSID (accessible via
$_COOKIE[PHPSESSID]
– On the server side, a matching temporary file is created
with the same name (sess_fc48gdbuckuj9oe3ef74i00b01)
6. Session Variables
• Session variables and the corresponding values are
stored in the temporary session file
• Database is not used – no connection nor query
needed
• You may access the session variables through the
$_SESSION superglobal
• Example: suppose session file contains:
– Count|s:7:”17”;
– Valid|s:7:”yes”;
– Count and Valid are session variables
– Count and Valid must first be added to $_SESSION
– Extract with $_SESSION[Count] and $_SESSION[Valid]
7. Operations to retrieve a session variable e.g.
$_SESSION[Count]
• PHP engine gets the value of
$_COOKIE[PHPSESSID] from the user cookie
• PHP engine finds a matching temporary
session file
• Inside the session file, the PHP engine looks
for Count variable and then extracts its value
i.e. 17
• $_SESSION[Count] get the value 17
8. To start a session:
• Just call session_start() function
• PHP does the rest:
– Sends the cookie to the user
– Creates the temporary session file
• See m17/17-1session.php
9. Registering and Modifying Session Variables
• See 17-2countme.php
<?php
session_start();
session_register('count');
/*
register a variable called count
After registering, as long as this session exists, a variable called
$_SESSION[count]
will be available. Initially it has no value.
*/
$_SESSION['count']++;
$msg ="<p>You've been here $_SESSION[count] times. Thanks!</p>";
?>
12. Getting more complicated . . .
• Managing user preferences with Sessions
– Start a session
– Ask the user for preferred font family and base
font size
– Display the preferences in subsequent pages
– Allow the user to change the preferences and
reset the values
– See 17-3session01.php
13. Starting a session and Registering Defaults
//PHP file for the defaults/preferences + html to request
preferences
<?php
//start a session
session_start();
/* Because the user may come back to reset, so we must
check if previous values exist.
If previous values of font-family and font_size are not
defined, assign default values
else extract the values from $_SESSION superglobal
*/
14. // Preparing for the CSS attributes
//check for stored values and register defaults
if ((!$_SESSION['font_family']) || (!$_SESSION['font_size'])) {
$font_family = "sans-serif";
$font_size = "10";
$_SESSION['font_family'] = $font_family;
$_SESSION['font_size'] = $font_size;
} else {
//extract from $_SESSION superglobal if exist
$font_family = $_SESSION['font_family'];
$font_size = $_SESSION['font_size'];
}
?>
15. Requesting and setting
Preferences
• HTML portion, see 17-3session01.php
– Display the default/previous preferences set by
CSS styles
– Requests for new preferences in a form
– Transmit the new preferences to
17-3session02.php
• 17-3session02.php
– Set the new preferences in $_SESSION