RU beehive logo ITEC dept promo banner
ITEC 325
2015spring
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect29-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?

Practical Issues


1

In fact, it's conceivable that two different browsers might share cookies — e.g. Firefox and Mozilla intentionally using the same cookie directory. So your server-side code shouldn't really care/notice if the user manages to “migrate” a session from one browser to another; your server probably shouldn't store (say) the browser-type in $_SESSION, nor any other browser-specific info (at least, not info that it takes too seriously).

Besides, in that example (browser-type), the correct solution happens to be checking the http header's User-Agent field.

     

homelectsexamshws
D2Lbreeze (snow day)


©2015, Ian Barland, Radford University
Last modified 2015.Apr.02 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.