|
home—lects—exams—hws
D2L—breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
As larger more complex web sites are being built the limitation of http as a stateless protocol becomes a problem. Web developers have no built in (html) method of remembering data from one page of an application to the next. This is a serious short-coming, e-commerce systems, user registration and login systems, and other online services rely on this functionality. Fortunately, maintaining state from one page to another is fairly simple using PHP.
Don't make two different cookies with the same path, but different domains
(one a superset of the other).
Different browsers may choose differently, which one gets sent.
(AFAICT: the more specific path wins; but for same paths with
two applicable domains, the first cookie set made wins.)
(It's not exactly advisable to make two different cookies with
the same name but different paths either, though that may not be enforceable,
e.g. /~ibarland and /~jcdavis may each contain
different scripts that happen to use the cookie “monster”.
Note also that if I set a cookie's path to be /,
then this is potentially a security flaw:
if somebody visits my script and I set a cookie secret-code-word
with server&path being
This explains why you have to call
It also explains why:
You must call
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> |
$_SESSION['first_name'] = "Marc"; $_SESSION['age'] = 35; |
Gotcha:setcookie sets a cookie relative to the current directory, but the session cookie (by default) is relative to root. So to destroy the session cookie, you must pass'/' (by default) as the 4th argument tosetcookie .Better, rather than assuming that session-cookies use / as the cookie-path, you should look up what the path for session-cookies is, using
session_get_cookie_params :
$myParams = session_get_cookie_params(); echo "The default path of cookies is ", $myParams['path']; print_r($myParams); setcookie( session_name(), '', 1, $myParams['path'] ); // note that sadly, php doesn't let you just do this in one line: // setcookie( session_name(), '', 1, session_get_cookie_params()['path'] ); // Syntax error!
Gotcha:session_start() puts a write-lock on the session file, so any other process trying to access it will block. (In particular: that 'other process' might be yourself, via an included-file which is also doing asession_start() !) One possible workaround (and good practice regardless): As soon as you're done updating any persistent values to$_SESSION , callsession_commit . This writes and closes the data file, although it doesn't unset data in memory so you can still refer to$_SESSION . (Just remember that any later changes made to the array are no longer being committed, and because you've given up the lock others might be updating the file w/o you seeing any changes. You can apparently callsession_commit again later, though presumably if some other process is accessing the file it may block and then it will overwrite the other file's changes.)
IMPORTANT:
If your session allows access to any secure information
(for instance, once a user successfully types a password,
you set
But we don't create that cookie ourselves!
We'd just called
php_ini('session.cookie_secure',true) |
IT-AUTH -- not released to students, since it enables students to create popular RU pages that authenticate and surreptitiously record each user's password.
The solution is clever: Your page has a form that submits to https://php.radford.edu/~it-auth/vsp09/login.php. Your form passes in two strings (via hidden inputs, presumably):
Don't tempt an honest man.
To be effective, you'd better use a cookie-name that can't be guessed —
in particular, if I use your site and then check my own cookies,
hopefully I won't be able to look at that cookie name/value and
guess what somebody else's cookie name/value is!
https://php.radford.edu/~it-auth/vsp09/tutorial.php
home—lects—exams—hws
D2L—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Mar.26 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |