Forum Moderators: coopster

Message Too Old, No Replies

How can I test whether the browser accepts cookies in php?

Cookie Browser Check One Page only

         

knnknn

12:10 pm on Oct 18, 2004 (gmt 0)

10+ Year Member



I know I can make a test by setting the cookie on one .php page and then reading the cookie on another.

But I want to be able to check it on 1 .php page alone.

Is that possible?

coopster

12:49 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, use the isset() [php.net] function to check for it's existence:
if (isset($_COOKIE['mycookie'])) { 
echo $_COOKIE['mycookie'];
} else {
echo 'My cookie not set yet';
}
Some other resources:
Cookies [php.net]
set_cookie [php.net]
$_COOKIE [php.net]

knnknn

1:55 pm on Oct 18, 2004 (gmt 0)

10+ Year Member



Sorry, I think I didn't state clear what I need.

I want to test, whether a browser (IE, Opera,...) accepts cookies.

The usual procedure is to setcookie and then to check isset.

But this is only possible on 2 pages (or after reloading the page), as is written at: [php.net...]
"If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie."

coopster

2:48 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the user did not accept the cookie, then there will be no $_COOKIE variables. Therefore, by checking to see if the variable is set, you will know whether or not the user accepted the cookie.

knnknn

7:12 pm on Oct 18, 2004 (gmt 0)

10+ Year Member



OK, let me state again.

setcookie( "test", 'something', time() + 30000);
if (isset($_COOKIE["test"]))

DOES NOT WORK.

kumarsena

7:19 pm on Oct 18, 2004 (gmt 0)

10+ Year Member



can we see teh exact code ur using?

killroy

7:22 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it's probably impossible on a single page, but cehck for an empty cookie= variable. Perhaps if cookies are not possible no such variable is returned.

Also you could try some JS magic, so it doesn't happen on a reload, but loading an internal hidden iframe or image file, or even external JS file.

To get fancy, set cookie, load an external file at the beginning of the page, such as an empty JS file, have the original script wait, hope the browser starts gettign teh JS without finishing the html, cehck for the cookie when JS is requested, set a falg on the server to continue sending the html. But be carefull to include security timeouts.

SN

Timotheos

8:00 pm on Oct 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the manual
[php.net...]

Common Pitfalls:

Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires.

That said, there's a script on this page in the user notes that checks if the browser accepts cookies. Hopefully it's what you need.