DutchBoy01 Posted October 29, 2009 Share Posted October 29, 2009 I'm very much a PHP newbie. I'm currently at the end of Ch 2 of Wrox Begining PHP6 Apache MySQL6 and am working thru the Excersises. Files: FontColorFamSize1.php & FontColorFamSize2.php File FontColorFamSize2.php operates error free when lines 3 thru 5 & 26 thru 34 are uncommented and lines 7 thru 11 & 37 thru 45 are commented. Works with sessions not with cookies. Any help much appreciated. ---------------FontColorFamSize1.php-------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Font color, Font Family, Font Size</title> </head> <form action="FontColorFamSize2.php" method="post" name="FontColorFamSize" target="_self"> <input name="FontFam" type="radio" value="SanSerif" checked />I prefer Sans-Serif fonts</br> <input name="FontFam" type="radio" value="Serif" />I prefer Serif fonts<br><br /> <select name="FontColor" size="3"> <option value="red" SELECTED>Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select><br /><br /> <select name="FontSize" size="3"> <option value="9" SELECTED>9</option> <option value="10pt">10pt</option> <option value="11pt">11pt</option> <option value="12pt">12pt</option> <option value="13pt">13pt</option> <option value="14pt">14pt</option> <option value="15pt">15pt</option> <option value="16pt">16pt</option> <option value="17pt">17pt</option> <option value="18pt">18pt</option> <option value="19pt">19pt</option> <option value="20pt">20pt</option> <option value="21pt">21pt</option> </select><br /><br /> <textarea name="TextArea" cols="40" rows="20"></textarea><br /><br /> <input name="SavePref" type="checkbox" value="1" />Save my font selections in a cookie.<br /><br /> <input name="Submit" type="submit" value="Submit" /> </form> <body> </body> </html> ---------------FontColorFamSize2.php-------------- <?php session_start(); /*$_SESSION['FontFam'] = $_POST['FontFam']; $_SESSION['FontColor'] = $_POST['FontColor']; $_SESSION['FontSize'] = $_POST['FontSize'];*/ if (isset($_POST['SavePref'])) { setcookie('font', $_POST['FontFam'], time() + 60); setcookie('size', $_POST['FontSize'], time() + 60); setcookie('color', $_POST['FontColor'], time () + 60); } if(isset($_POST['SavePref'])) { $SavePref = 1; } else { $SavePref = 0; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Font color, Font Family, Font Size</title> <?php /*$myFontFam = $_SESSION['FontFam']; if ($myFontFam == 'SanSerif') { $myFontFamilyNames = "Arial, Helvetica, sans-serif"; } else { $myFontFamilyNames = '"Times New Roman", Times, serif'; } $myFontColor = $_SESSION['FontColor']; $myFontSize = $_SESSION['FontSize']; $myText = $_POST['TextArea'];*/ ?> <?php $myFontFam = $_COOKIE['FontFam']; if ($myFontFam == 'SanSerif') { $myFontFamilyNames = "Arial, Helvetica, sans-serif"; } else { $myFontFamilyNames = '"Times New Roman", Times, serif'; } $myFontColor = $_COOKIE['FontColor']; $myFontSize = $_COOKIE['FontSize']; $myText = $_POST['TextArea']; ?> <style type="text/css"> <!-- .style1 { font-family: <?PHP echo $myFontFamilyNames; ?> ; color: <?PHP echo $myFontColor; ?> ; font-size: <?PHP echo $myFontSize; ?> ; </style> </head> <body> <?php echo "SavePreferences = "; echo $SavePref ; echo "<br /><br />"; echo "FontFam = "; echo $myFontFam ; echo "<br /><br />"; echo "FontColor = "; echo $myFontColor ; echo "<br /><br />"; echo "FontSize = "; echo $myFontSize ; echo "<br /><br />"; echo '<span class="style1">'; echo $myText ; echo "</span>"; echo "<br /><br />"; ?> <span class="style1">I went to walmart and bought diapers.</span> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/179545-works-with-session-not-with-cookie/ Share on other sites More sharing options...
MadTechie Posted October 29, 2009 Share Posted October 29, 2009 First off. Welcome to PHP Freaks Now when posting code please use the code tags (#) Now on to the code.. SESSIONS (as these work I'm break down WHY they work) Okay we set the SESSIONS FontFam, FontColor & FontSize to their respective values, ie the SESSION FontFam = the POSTed FontFam $_SESSION['FontFam'] = $_POST['FontFam']; $_SESSION['FontColor'] = $_POST['FontColor']; $_SESSION['FontSize'] = $_POST['FontSize']; Now we set $myFontFam to the SESSION FontFam etc $myFontFam = $_SESSION['FontFam']; if ($myFontFam == 'SanSerif') { $myFontFamilyNames = "Arial, Helvetica, sans-serif"; } else { $myFontFamilyNames = '"Times New Roman", Times, serif'; } $myFontColor = $_SESSION['FontColor']; $myFontSize = $_SESSION['FontSize']; $myText = $_POST['TextArea']; OKAY.. now lets take a look at cookies COOKIES Okay we set the COOKIES font, size & color to their respective values, (humm not the same as SESSION) if (isset($_POST['SavePref'])) { setcookie('font', $_POST['FontFam'], time() + 60); setcookie('size', $_POST['FontSize'], time() + 60); setcookie('color', $_POST['FontColor'], time () + 60); } Now we set $myFontFam to the COOKIE FontFam etc .. wait one second.. has COOKIE FontFam been set ? $myFontFam = $_COOKIE['FontFam']; if ($myFontFam == 'SanSerif') { $myFontFamilyNames = "Arial, Helvetica, sans-serif"; } else { $myFontFamilyNames = '"Times New Roman", Times, serif'; } $myFontColor = $_COOKIE['FontColor']; $myFontSize = $_COOKIE['FontSize']; $myText = $_POST['TextArea']; So we can fix it by either setting $myFontFam = $_COOKIE['font']; OR (PREFERRED) Setting $_COOKIE['FontFam'] instead of setting $_COOKIE['font'] I hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/179545-works-with-session-not-with-cookie/#findComment-947466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.