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

homelectshws
D2Lbreeze (snow day)

sessions
sessions
(chapt. 9)

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.)

youtube (26m00s):

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?
youtube (16m42s):

Security Considerations

Practical Issues


1

In fact, it's conceivable that two different browsers might share cookies — e.g. imagine if Firefox and Mozilla intentionally used the same directory to store cookie-info. 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 this example about browser-type, you wouldn't want to store that as session info, but instead just use the http header's User-Agent field. Your PHP code can retrieve that information through the superglobal $_SERVER.

     

homelectshws
D2Lbreeze (snow day)


©2017, Ian Barland, Radford University
Last modified 2017.Oct.30 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.