tags:

views:

242

answers:

3

Can someone show me a simple way I can test if someone's cookies are not enabled in php? I want to create a test script to determine if the user can properly use my site. If they cannot, I will redirect them to a couple screen shots to show them what to change.

I am only using $_SESSION[] variables, and I beleive that the only thing the cookie is for, in my setup is the session id variable.

Ideally, I would like to turn on an option in PHP for that user to pass the session id back and forth via GET/POST if their cookies are messed up.

Anything Javascript will not work for me, as they may not have javascript enabled.

A: 

use SID
see http://jp2.php.net/function.session-start and http://jp2.php.net/manual/en/session.idpassing.php

SID will be empty if the cookie was accepted, and contain the session id (PHPSESSID=session_id(), actually) otherwise. This does however depend on compilation flags so be sure to check the refs.

you could use it as :

echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
Jonathan Fingland
+3  A: 
setcookie("cookies","yes",time() +"3600");

Then redirect after that and test if the cookie is set on the next page. It will take at least two pages to know whether or not cookies are enabled.

altCognito
Correct, as the manual says the cookie wont be visible until another page is loaded after it has been set. http://www.php.net/manual/en/function.setcookie.php
Marc
A: 

The standard way of doing this is by using "the proof is in the pudding" programming.

There are two ways to do this:

  • If testing for cookies without Javascript, issue a cookie and read it via PHP after a redirect. If it's there, fine. If not...you know cookies are not enabled.
  • If you can use Javascript, issue a cookie via PHP and read it using Javascript (I think the cookie will be there on the first page load as the cookie would have been set if browser gets the setcookie header).
Webber