
Originally Posted by
jscheuer1
Cookies are a little tricky to work with but, [QuirksMode] has boiler plate code for setting reading and deleting cookies.
It has code for setting cookies client-side, but the OP should be doing it server-side.
PHP Code:
/* Has a cookie already been set? If so, was
* it set for this page?
*/
if(isset($_COOKIE['uri']) && ($_SERVER['REQUEST_URI'] === $_COOKIE['uri'])) {
/* Yes, so check how many times it's been
* visited.
*
* Note that the 'visits' value will
* lag: it's set to '1' on the first visit,
* but will still be '1' on the second
* (until the else branch below).
*/
if('3' === $_COOKIE['visits']) {
/* This page has been revisited three
* times (four visits in total), so
* delete the cookies and redirect.
*/
header('HTTP/1.1 302 Found');
header('Location: http://www.example.com/');
setcookie('uri', '', 0);
setcookie('visits', '', 0);
} else {
/* Increment the visit count and
* continue.
*/
setcookie('visits', (string) $_COOKIE['visits'] + 1);
}
} else {
/* This is the first visit (or the first
* since a previous redirect), so setup
* for future encounters.
*/
setcookie('uri', $_SERVER['REQUEST_URI']);
setcookie('visits', '1');
}
This could be used in multiple places as it stores where the cookie was set. If it's different from the current location, then the cookie is reset. Be aware though, that
  /index.php
and
  /index.php?name=value
will be considered different for the purposes of this test, though that's probably not an issue.
Hope that helps,
Mike
Bookmarks