PHP - Cookies not setting

Associate
Joined
25 Jul 2005
Posts
430
Hi. I've tried to copy something I had in an old site to a new one I'm making. It's a simple stylesheet selector and it worked fine on the old site.
After finding out it didn't work, I tried assigned some cookie variables and tried to echo them in another page, just to test it and that didn't work either.
I guess I need to change some of the server settings, but I don't know php well enough to know what to ask for :p
Any idea which things need changing?
I can post the contents of the php.php file if needed.
Thx.
 
May well be related to register_globals i.e. it was enabled on your old site, but is now disabled (as it should be). Cookie variables are accessed from $_COOKIE, so use:
Code:
<?php echo $_COOKIE['selectedStyle']; ?>
rather than
Code:
<?php echo $selectedStyle; ?>
Might well be a good idea to post the whole script up.
 
OK. At the top of the page I have:
<?


if (isset($_COOKIE["selectedStyle"])) // has the cookie already been set
{
$style=$_COOKIE["selectedStyle"];
}else{
$style = 0;
}

if (isset($_POST["changeStyle"])) // changing the style
{
$style=$_POST["changeStyle"];
}

setcookie("selectedStyle",$style, time()+36000, "/"); // update or create the cookie
?>

Then within the head tags I have this:
<LINK REL="stylesheet" HREF="stylesheet<?php echo $style; ?>.css">

And finally the form:
<form method="post" action="<?= $_SERVER["PHP_SELF"];?>">
<P class="general">Normal<input type="radio" name="changeStyle" value="0" checked><BR></P>
<P class="general">Text Only (No Flash Content)<input type="radio" name="changeStyle" value="1"><BR></P>

<INPUT type="submit" name="submitstyle" value="Change"><BR>
</form>

Every other page simply has the following at the top (as well as the head section):
<?
if (isset($_COOKIE["selectedStyle"])) // if style has been set, use selected style
{
$style=$_COOKIE["selectedStyle"];
}
else // if style not yet set, default to 0
{
$style=0;
}
?>

When it reloads the page immediately after using the form, it works (but no cookies are used to achieve this), however, as soon as I load another page (or even the same one) it goes back to the default value.

EDIT - Just checked register globals is on for both sites. :confused:
 
Last edited:
I just noticed the obvious error: you're setting the cookie unconditionally on every page, which triggers the following (taken from the PHP manual):

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. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

Wrap the setting of the cookie in a conditional:

PHP:
if(!isset($_COOKIE['selectedStyle']))
	setcookie('selectedStyle', $style, time()+36000, '/');

That should fix it.
 
This is untested, but give it a go.

Form code, just for reference:
PHP:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; // Short tags are evil ?>">
 <p class="general">Normal<input type="radio" name="changeStyle" value="0" checked></p>
 <p class="general">Text Only (No Flash Content)<input type="radio" name="changeStyle" value="1"></p>
 <p><button type="submit" name="submitstyle">Change</button></p>
</form>

At the top of each page:
PHP:
<?php // Short tags are evil

/*
* Put at the top of every page
*/
if(isset($_POST['changeStyle']))
{
	$style = ($_POST['changeStyle'] == 0)
		? 0
		: 1; // Don't use the POST value, simply check it - for security
	/*
	* If you want to make the page reload to put the cookie into effect immediately:
	* header('location: ' . $_SERVER['PHP_SELF']);
	*/
}
else
	$style = 0; // Whatever the default is

if(!isset($_COOKIE['selectedStyle']))
{
	setcookie('selectedStyle', $style, time()+36000, '/');
}

?>

Then the stylesheet link:
PHP:
<link rel="stylesheet" type="text/css" href="<?php echo $_COOKIE['selectedStyle']; ?>">
 
Last edited:
Back
Top Bottom