note 39651 added to function.setcookie

From: Date: Fri, 06 Feb 2004 04:31:09 +0000
Subject: note 39651 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-64711@lists.php.net to get a copy of this message
Here's a good way to check that the host browser accepts cookies. (this one works, trust me!) It includes a P3P header, so that the cookies are not blocked by IE6, unless the user has explicitly requested that cookies be blocked. It uses a query string to see if the page has been re-loaded, which gets around the "cookie is only visible after refreshing the page" problem. It also preserves any existing query strings, so will still work if you include it on a page like "cookiesetter.php?product=milk&price=3.50" testcookie.php <?php header("P3P: CP=\"ALL DSP COR NID CURa OUR STP PUR\""); $nocookies = false; $reloaded = $_GET["ckset"]; $url = $_SERVER[PHP_SELF]; $querystring = $_SERVER[QUERY_STRING]; $querystring = str_replace("ckset=ok", "", $querystring); setcookie("check_cookie", "on", time()+(1000000)); if ($reloaded == "" and !isset($check_cookie)) { echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;URL=http://www.naturalanimalcare.com" . $_SERVER[PHP_SELF] . "?ckset=ok" . $querystring . "\">"; exit; } else { if (!isset($check_cookie)) $nocookies = true; } ?> To use this, save it as "testcookie.php" and then the page you want to test cookies in would go something like this ... <?php include "testcookie.php"; // Test for cookies // Other setcookie commands go here... // // Headers, layout, etc go here... // // Body text ... If ($nocookies) { echo "Your browser is not accepting cookies.<br> Please turn them on or update your browser"; } else { echo "Your browser is accepting cookies"; // Rest of the page goes here... } // Footers, etc. ?> ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39651 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39651&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39651&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64711) next »