Forum Moderators: coopster
--------------------------------
<?php
if ($BeenSubmitted) {
setcookie("BGColor", "$NewBGColor");
setcookie("TextColor", "$NewTextColor");
$BGColor = $NewBGColor;
$TextColor = $NewTextColor;
} else {
if (!$BGColor) {
$BGColor = "WHITE";
}
if (!$TextColor) {
$TextColor = "BLACK";
}
}
?><HEAD>
<TITLE>User Customization</TITLE>
</HEAD>
<?php
print ("<BODY BGCOLOR=$BGColor TEXT=$TextColor>\n");
?>
Currently your page looks like this!
<FORM ACTION="cookies.php" METHOD=POST>
Select a new background color:
<SELECT NAME="NewBGColor">
<OPTION VALUE=WHITE>WHITE</OPTION>
<OPTION VALUE=BLACK>BLACK</OPTION>
<OPTION VALUE=BLUE>BLUE</OPTION>
<OPTION VALUE=RED>RED</OPTION>
<OPTION VALUE=GREEN>GREEN</OPTION>
</SELECT>
Select a new text color:
<SELECT NAME="NewTextColor">
<OPTION VALUE=WHITE>WHITE</OPTION>
<OPTION VALUE=BLACK>BLACK</OPTION>
<OPTION VALUE=BLUE>BLUE</OPTION>
<OPTION VALUE=RED>RED</OPTION>
<OPTION VALUE=GREEN>GREEN</OPTION>
</SELECT>
<INPUT TYPE=HIDDEN NAME=BeenSubmitted VALUE=TRUE>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
</FORM>
</BODY>
</HTML>
This code I modified (without cookies) from the above
-----------------------------------
<HTML>
<HEAD>
<TITLE>User Customization</TITLE>
</HEAD>
<?php
if ($BeenSubmitted)
print ("<BODY BGCOLOR=$NewBGColor TEXT=$NewTextColor>\n");
else
print ("<BODY>");
?>
Currently your page looks like this!
<FORM ACTION="<?PHP echo $PHP_SELF;?>" METHOD=POST>
Select a new background color:
<SELECT NAME="NewBGColor">
<OPTION VALUE=WHITE>WHITE</OPTION>
<OPTION VALUE=BLACK>BLACK</OPTION>
<OPTION VALUE=BLUE>BLUE</OPTION>
<OPTION VALUE=RED>RED</OPTION>
<OPTION VALUE=GREEN>GREEN</OPTION>
</SELECT>
Select a new text color:
<SELECT NAME="NewTextColor">
<OPTION VALUE=WHITE>WHITE</OPTION>
<OPTION VALUE=BLACK>BLACK</OPTION>
<OPTION VALUE=BLUE>BLUE</OPTION>
<OPTION VALUE=RED>RED</OPTION>
<OPTION VALUE=GREEN>GREEN</OPTION>
</SELECT>
<INPUT TYPE=HIDDEN NAME=BeenSubmitted VALUE=TRUE>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
</FORM>
</BODY>
</HTML>
They both produce the same result, so what's the point of setting cookies?
Thanks
You can maintain state during a user's session on your site by using the session functions (session_start() - $_SESSION etc). The difference between the session vars and cookie vars is that the cookie can be stored on the user's PC indefinitely (depending on what they do with their cookies), whereas the session vars are only accessible so long as the browser is open and they are stored on the server (the user can't delete them).
whereas the session vars are only accessible so long as the browser is open and they are stored on the server (the user can't delete them).
between multiple pages
And between visits, if your cookie is persistent. So the next time they come back to your site, the cookie would be read and their preferences reinstated. Without the cookie, the visitor would have to go through the process of making their selections again ... and on the next visit ... and the next ... etc.
And between visits, if your cookie is persistent. So the next time they come back to your site, the cookie would be read and their preferences reinstated. Without the cookie, the visitor would have to go through the process of making their selections again ... and on the next visit ... and the next ... etc.
First of all, thanks for all your explanations!
In the case with cookies, so if I select red to be the background color and white to be the text color. Then I close the browser, then open it again. Should the browser go back to my selections earlier? I tried that, it didn't work. The browser went back to the default colors, which is white and white.