Hi All
If the below works, should I post it in the snippets section?
This is THE definitive method of testing if cookies are enabled on a user's PC, BEFORE loading the page for the user to view. It relies on Java being enabled on the user's PC.
All the other methods I have seen posted DO NOT work because they rely on setting a temporary cookie, and then checking if it is there. The problem with this is that when you set a cookie, you cannot read it until the page has loaded, and thus you cannot check it from the first page the user sees.
NOTE: I have not seen this method used anywhere else, but apologies if someone has done this before, I just couldn't find it!
Somehow, you need to load the page with no content, set the cookie, reload the page, check the cookie and display the page with whatever content is appropriate.
My first attempt reloaded the page with a variable passed in the URL (http://www.yourweb.com/index.php?status=yes). But, IF the user bookmarked this page or noted the variable, they could then bypass my test.
In the end, I used a hidden form with a post method, and used java to submit it. On re-entering the page after reload, a quick $_POST is used to fetch the variable (just in case register_globals is off) and then it is checked.
Your php file:
<?PHP
if ($POST['cookiecheck']) $cookiecheck=($POST['cookiecheck']);
if (!$cookiecheck||$cookiecheck!="yes")
{
setcookie("TEMPCOOKIE", "NOVALUE", time() + 60 * 60);
echo "
<form name=\"hiddenform\" method=\"post\">
<input name=\"cookiecheck\" type=\"hidden\" value=\"yes\">
</form>
<script>javascript:document.hiddenform.submit();</script>
";
exit;
}
else
{
echo "
<html>
<head>
<title>
</title>
</head>
<body>
YOUR BODY TEXT HERE
";
$cookieinfo = $HTTP_COOKIE_VARS["TEMPCOOKIE"];
if ($cookieinfo != "NOVALUE")
{
include("cookiewarning.inc.php");
}
echo "
MORE BODY TEXT HERE
</body>
</html>
";
}
?>
Trevor 🙂