RU beehive logo ITEC dept promo banner
ITEC 325
2012fall
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect08b-sessions
sessions
chapter 9

From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)

We have discussed cookies, and in particular how setting a cookie "userID" can help us (the server) keep track of repeated visits from the same browser&user. (If you leverage that with having the server keep a database for each user, then this gives us history that can cross browsers.)

Self-assessment: Why does the following not print out 2.50?

<?php
  // before any html has been printed:
  setcookie('hamburger-price', 2.50);
  ?>

  ⋮
  <p>
  The going rate for hamburgers is $<?php printf("%.2f", $_COOKIE['hamburger-price'] ); ?>.
  </p>
 
(More precisely, it may not print out 2.50...) Note that the printf format is fine.

Sessions

Sessions: Another way to add history to the state-less HTTP approach. Recall that we've been battling this problem in several different ways now:
  1. Single page, no state.
  2. Using POST/GET to pass state from one page to the next: add info to URL
  3. Using cookies: the browser passes a (variant) GET argument with a serial number
  4. Writing a file server-side (perhaps along with previous technique)
  5. sessions: an automated way of doing the previous: create/restore an array on each visit (using a file kept server-side, and client-side a cookie so the server knows which stored file should be used for a given HTTP request)
How do you use sessions in php?

A smaller example is at page0.php. A similar (but more varied) example is at lect09a-ch09-login.php.

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Nov.21 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme