Forum Moderators: coopster

Message Too Old, No Replies

what's the point of using cookies if I can do it without?

another Larry Ullman example

         

yellow_nemo

6:06 pm on May 5, 2005 (gmt 0)

10+ Year Member



This code (with cookies) is copied straight out from his book.

--------------------------------

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

ironik

10:27 pm on May 5, 2005 (gmt 0)

10+ Year Member



Cookies will allow you to maintain state between multiple pages, your example will change the colour after the form has been submitted, but on subsequent page loads (without the POST vars) then it will 'forget' those values.

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

jusdrum

10:43 pm on May 5, 2005 (gmt 0)

10+ Year Member



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

Actually if the user deletes their session cookie, they won't be able to access their data anymore, so essentially that will clear their browser from session data.

StupidScript

10:48 pm on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

killroy

10:07 am on May 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many different ways to do the same thing. Personally I keep track on the server side without using cookies. Using user_agents/IPs and so on to track users. Of coruse this has it's disadvantages, but so do cookies. No session tracking method is perfect, and all fail in some cases.

SN

yellow_nemo

3:58 pm on May 6, 2005 (gmt 0)

10+ Year Member



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.