20
Sessions session_start() function session_register(‘variabl e’) function $_SESSION[‘variable’]

17 sessions

Embed Size (px)

Citation preview

Page 1: 17 sessions

Sessions

session_start() functionsession_register(‘variable’) function

$_SESSION[‘variable’]

Page 2: 17 sessions

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.

Page 3: 17 sessions

Check with Windows Explorer

Page 4: 17 sessions

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$

Page 5: 17 sessions

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)

Page 6: 17 sessions

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]

Page 7: 17 sessions

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

Page 8: 17 sessions

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

Page 9: 17 sessions

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>";?>

Page 10: 17 sessions

Accessing 17-2countme.php

Refresh the page 6 more times:

Page 11: 17 sessions

Session temporary file

Session temporary file contains count|i:7;

Page 12: 17 sessions

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

Page 13: 17 sessions

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

*/

Page 14: 17 sessions

// 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'];

}

?>

Page 15: 17 sessions

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

Page 16: 17 sessions

Initially preferences not defined, defaults used

Page 17: 17 sessions

Selecting new preferences

Page 18: 17 sessions

Wingdings and font_size 12

Page 19: 17 sessions

Selecting Courier and size 14 pt

Page 20: 17 sessions

Courier and size 14pt