basic php error: cookies

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
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

basic php error: cookies

Post by snpo123 »

okay i am just begining to learn php and have a basic error. I am trying to get php to creatw two cookis after it checks if a form has been submitted, but instead of creating both cookies it only creates the second one. here is the script:

Code: Select all

<?php
$cookies=FALSE;
----------------------------------------------------------------
//This is the section I am having trouble with
if (isset ($_POST['Submit'])) {
setcookie ('fontcolor', $_POST['fontcolor']); 
setcookie ('bgcolor', $_POST['bgcolor']);

$cookies=TRUE;

}
?> 
----------------------------------------------------------------

<head>
<title>Customize Your Settings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($cookies) {
print '<p>Your settings have been entered! Click <a href="viewsettings.php">here</a> to go back to the main page.</p>';
}
?>
<form name="settings" method="post" action="customize.php">

Select your font color:<br>
<select name="fontcolor">
<option value="#0000FF">Blue</option>
<option value="#FF0000">Red</option>
<option value="#00FF00">Green</option>
<option value="#FFFF00">Yellow</option>
<option value="#FF8000">Orange</option>
<option value="#8000FF">Purple</option>
<option value="#000000">Black</option>
<option value="#FFFFFF">White</option>
<option value="#8080FF">Teal</option>
</select><br>


<p>Select your background color:<br>

<select name="bgcolor">
<option value="#0000FF">Blue</option>
<option value="#FF0000">Red</option>
<option value="#00FF00">Green</option>
<option value="#FFFF00">Yellow</option>
<option value="#FF8000">Orange</option>
<option value="#8000FF">Purple</option>
<option value="#000000">Black</option>
<option value="#FFFFFF">White</option>
<option value="#8080FF">Teal</option>
</select>


</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>

I know that all the syntax is correct and that i named all the variables and cookies correctly, but for some reason php will only create the second cookie. I know that only the second cookie is created because even when i switch the places of the two cookies at the top the second one is the only one that is created. Will someone please help me with this, because i am really confused and have no idea what to do.

[Edit: Added PHP tags for eyecandy. --JAM]
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well whether this is the problem or not: you should always assign a time() value to cookies. Otherwise it'll delete itself as soon as the browers closes

setcookie('tim', $var, time() +3600);
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

i tried doing that, and it worked for a short while, but then it stopped and just went back to normal. Even after i reset the cookies it didnt work. Here is what I am trying to do. I am trying a make a page that lets your customize and reset your background and text colors. it is located on this page: http://snpo123.mvhosted.com/viewsettings.php

when i say to set two cookies, only the second cookie i say to be made is made. this is getting confusing...
User avatar
snpo123
Forum Commoner
Posts: 77
Joined: Sat Apr 17, 2004 6:31 pm

Post by snpo123 »

help? anyone?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You need to post the code on where you later retrieve the $_COOKIE, because the cookies are being set.
Post Reply