Upload
abhijit-gaikwad
View
83
Download
3
Tags:
Embed Size (px)
Citation preview
Sessions
session_start() functionsession_register(‘variable’) function
$_SESSION[‘variable’]
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-pathsession.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.
Check with Windows Explorer
For cs346.cs.uwosh.edu
/var/lib/php5 already exists and is empty
huen@CS346:/var/lib/php5$ sudo pwd/var/lib/php5huen@CS346:/var/lib/php5$ sudo ls -ltotal 0huen@CS346:/var/lib/php5$
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)
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]
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
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
Registering and Modifying Session Variables
• See 17-2countme.php<?phpsession_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>";?>
Accessing 17-2countme.php
Refresh the page 6 more times:
Session temporary file
Session temporary file contains count|i:7;
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
Starting a session and Registering Defaults//PHP file for the defaults/preferences + html to request
preferences<?php//start a sessionsession_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 valueselse extract the values from $_SESSION superglobal
*/
// Preparing for the CSS attributes
//check for stored values and register defaultsif ((!$_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'];
}
?>
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
Initially preferences not defined, defaults used
Selecting new preferences
Wingdings and font_size 12
Selecting Courier and size 14 pt
Courier and size 14pt