Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if cookies are enabled

Status
Not open for further replies.

rob51383

Programmer
Jun 23, 2004
134
US
While toying around I discovered a work-around to the problem of not being able to check if cookies are enabled untill a page reload is done. This method is almost completely hidden from the user. Let me know what you think =)

Code:
<?php
/**
 *First set your cookie.
 */

$value = "Cookie Check";
setcookie ('cookie', $value, 86400, '/', '.yourdomain.com', 0);



if(isset($_COOKIE['cookie']))
	{

	/**
	 *If the cookie is there, do something with the value
	 */

	$PHPSESSID = $_COOKIE['cookie'];

	}
elseif($_GET['set'] != 'yes')
	{

	/**
	 *If set does not = 'yes' redirect them to the same script
	 *where it will = 'yes'!
	 */

	header ("Location: _test1.php?set=yes");

	}
else
	{

	/**
	 *The cookie is not set, the redirect done,
	 *and they do not have cookies. Tell them to enable them!
	 */

	header ("Location: index.html");
	}
?>
 
...a work-around to the problem of not being able to check if cookies are enabled untill a page reload is done
But surely that's exactly how you are "working around it" -- you are reloading the page:
Code:
header ("Location: _test1.php?set=yes");
Maybe I'm missing something here.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
But surely that's exactly how you are "working around it" -- you are reloading the page:

Maybe I'm missing something here.

This is a TIP post for people doing a PHP forum search for "Check if cookies are enabled" because I did not find any good ones for that query. If you do not like the method, post a better one.
 
There is a gotcha if you want to run this script under most versions of IIS. IIS does not allow cookies to be sent when the "Location" HTTP header is used.

The workaround, believe it or not, is to make sure the script's name begins with "nph-".

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
ahhh non-parsed-headers !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top