Cookies not setting

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

Post Reply
Magicmoose
Forum Newbie
Posts: 2
Joined: Fri Apr 06, 2007 9:46 am

Cookies not setting

Post by Magicmoose »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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 assigning 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


At the top of the page I have:

Code: Select all

<?


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:

Code: Select all

<LINK REL="stylesheet" HREF="stylesheet<?php echo $style; ?>.css">
And finally the form:

Code: Select all

<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):

Code: Select all

<?
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.
Any idea which things need changing?
Thx


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you checked that cookies are setting in your browser?
Magicmoose
Forum Newbie
Posts: 2
Joined: Fri Apr 06, 2007 9:46 am

Post by Magicmoose »

Yes. I've tried it on 5 or 6 different browsers.
Post Reply