No Cookie Redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

No Cookie Redirect

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: No Cookie Redirect

Post 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;
}
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: No Cookie Redirect

Post by koolsamule »

Coolio . . .I'll give that a go, many thanks!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: No Cookie Redirect

Post by Benjamin »

:arrow: Duplicate = Locked
Locked