Page 1 of 1

No Cookie Redirect

Posted: Mon Jun 14, 2010 4:01 pm
by koolsamule
Hi Chaps,

Quick question on this one, I've got a <noscript> tag in my HTML to check to see if Javascipt in enabled, but how do you check to see if cookies are enabled?
Is there HTML code / PHP function, or do you simply try to set a cookie and redirect on failure to read the cookie you just to to set?

Cheers

Re: No Cookie Redirect

Posted: Mon Jun 14, 2010 4:16 pm
by requinix
If someone has cookies disabled then not only will 99% of the Internet not work for them, they'll probably have JavaScript disabled too (accounting for the other 1%).

1. You can't check if setting a cookie succeeded in PHP
2. You can't rely on JavaScript to set a cookie

There are a few ways to check - not all perfect - but most tend to involve (a) setting a cookie and (b) loading a page specifically designed to look for the cookie you just set. A rough example:

Code: Select all

if (isset($_GET["cookie"]) && !isset($_COOKIE["foo"])) {
    // probably has cookies disabled
} else if (!isset($_COOKIE["foo"])) {
    setcookie("foo", "bar", $etc);
    header("Location: /path/to/this/file.php?cookie=");
    exit;
}

Re: No Cookie Redirect

Posted: Tue Jun 15, 2010 6:23 am
by koolsamule
Coolio . . .I'll give that a go, many thanks!

Re: No Cookie Redirect

Posted: Fri Jun 18, 2010 7:05 am
by Benjamin
:arrow: Duplicate = Locked