cforrester Posted May 22, 2009 Share Posted May 22, 2009 Hi there folks. I'm new to the site and new to PHP. I look forward to becoming an active member of this community. Now, I have recently bought a book to teach myself PHP and one of the exercise questions has 2 parts. Part 1 asks me to enter text and select font, size and colour and return the text entered using the settings chosen. This I have achieved without much effort by passing the values via $_SESSION values. Part 2 expands on this by asking the user if they wish to save the data to a cookie for later use. This switch is set via a checkbox. My problem is that when I try call up this data from the cookie, nothing appears. Below is my code for the 2 files (input and output). Can anyone tell me what I am doing wrong? The Input File <html> <head> <title>Calling text formatting from PHP stored variables and applying to entered text</title> </head> <body> <form method="post" action="textoutput.php"> <!-- Input text and select colour, font & size --> <p> Enter your test text: <input type="text" size="60" name="testtext" /></p> <p> Select a text colour: <select style="width: 150px;" id="textcolour" name="textcolour" /> <option value="red">Red</option> <option value="purple">Purple</option> <option value="blue">Blue</option> <option value="black">Black</option> </select></p> <p> Select a font type: <select style="width: 150px;" id="fonttype" name="fonttype" /> <option value="Verdana">Verdana</option> <option value="New Times Roman">New Times Roman</option> <option value="Arial">Arial</option> </select></p> <p> Select a font size: <select style="width: 150px;" id="fontsize" name="fontsize" /> <option value="10px">10pt</option> <option value="12px">12pt</option> <option value="16px">16pt</option> <option value="20px">20pt</option> </select></p> <p> <label for="savedata">Save as a cookie?<label> <input type="checkbox" id="savedata" name="savedata" /> </p> <input type="submit" name="process" value="Process" /> </form> </body> </html> </html> The Output File <?php if (isset($_POST['savedata'])) { setcookie("testtext", $_POST['testtext'],time()+60); setcookie("textcolour", $_POST['textcolour'],time()+60); setcookie("fonttype", $_POST['fonttype'],time()+60); setcookie("fontsize", $_POST['fontsize'],time()+60); } /* session_start(); Removed to see if the cookie is saving $_SESSION['testtext'] = $_POST['testtext']; $_SESSION['textcolour'] = $_POST['textcolour']; $_SESSION['fonttype'] = $_POST['fonttype']; $_SESSION['fontsize'] = $_POST['fontsize']; */ ?> <html> <head> <title>The Output of textinput.php</title> </head> <body> <p>The following text was entered and processed using the options you set for it:</p> <p <?php echo 'style="color:' . $_COOKIE['textcolour'] . '; ' . 'font-family:' . $_COOKIE['fonttype'] . '; ' . 'font-size:' . $_COOKIE['fontsize'] . ';"'; ?>> <?php echo $_COOKIE['testtext']; ?></p> </body> </html> Thank you in advance. Clinton Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/ Share on other sites More sharing options...
Brian W Posted May 22, 2009 Share Posted May 22, 2009 Not sure why is wouldn't save the cookie data, make sure it is even getting into your save statement... if (isset($_POST['savedata'])) { die('in') setcookie("testtext", $_POST['testtext'],time()+60); setcookie("textcolour", $_POST['textcolour'],time()+60); setcookie("fonttype", $_POST['fonttype'],time()+60); setcookie("fontsize", $_POST['fontsize'],time()+60); } die('not in'); if when you check the box and submit it returns "not in", you have a problem and we can look further into the conditional statement. Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840319 Share on other sites More sharing options...
jackpf Posted May 22, 2009 Share Posted May 22, 2009 You could try setting a cookie like so: setcookie("testtext", $_POST['testtext'],time()+60, '/', 'www.yourdomain.com'); With the extra arguments of the path and domain name. They're not required but idk...could help. If that doesn't work, just try setting a cookie normally and see if it works. Also, try something like this: if (!isset($_POST['savedata'])) { die('the checkbox is not set!'); } else { die('the checkbox is set'); } Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840320 Share on other sites More sharing options...
MadTechie Posted May 22, 2009 Share Posted May 22, 2009 as jackpf says try the including the domain etc.. also (sounds strange) but try another browser (IE and mainly Safari) has issules with cookies Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840322 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 I changed the cookie declaration statement to the following: if (isset($_POST['savedata'])) { die('The check box is set'); setcookie("testtext", $_POST['testtext'],time()+60); setcookie("textcolour", $_POST['textcolour'],time()+60); setcookie("fonttype", $_POST['fonttype'],time()+60); setcookie("fontsize", $_POST['fontsize'],time()+60); } else die('The check box is not set'); It returned the correct status of the check box so I am guessing it has to do with the actual cookie declaration. You mention putting in my domain. As I am only learning and do no have a domain, would I just enter "localhost" in place of "www.mydomain.com"? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840350 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 yep, localhost is a domain Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840352 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 Setting the domain as "localhost" doesn't work for me. I always define the domain as FALSE on my local box. Seems to work for me. Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840356 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 Right, still not working. I also rechecked my syntax and replaced the " with ' in the cookie name declaration. I also figured I might try just echoing just the values of each cookie instead of trying to assign the values to a style and changed my coding to the following: <?php echo $_COOKIE['textcolour']; echo $_COOKIE['fonttype']; echo $_COOKIE['fontsize']; echo $_COOKIE['testtext']; ?> I still have had no luck with output there either. ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840359 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 try a simple test <?php setcookie("test","hello",time()+24*60*60); echo $_COOKIE['test']; ?> oh and Slightly off topic.. create a domain.. which is also local oooow! okay go Start->Run-> %windir%\system32\drivers\etc\hosts select notepad your get afile like this 127.0.0.1 localhost now add 127.0.0.1 www.mylovelysite.com save and close now goto http://www.mylovelysite.com and look its localhost soo now www.mylovelysite.com is your domain Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840363 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 Ok, test script works. I get a hello back. Also created a domain now. Added the domain name into the cookie declaration as shown by jackpf. Still not working. Could the problem be with my php.ini file and how it deals with cookies? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840375 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 okay cool let update your script to a expiry of 1 hour setcookie("testtext", $_POST['testtext'],time()+60); to setcookie("testtext", $_POST['testtext'],time()+1*60*60); Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840376 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 Try this <html> <head> <title>Calling text formatting from PHP stored variables and applying to entered text</title> </head> <body> <form method="post" action="textoutput.php"> <!-- Input text and select colour, font & size --> <p> Enter your test text: <input type="text" size="60" name="testtext" /></p> <p> Select a text colour: <select style="width: 150px;" id="textcolour" name="textcolour" /> <option value="red">Red</option> <option value="purple">Purple</option> <option value="blue">Blue</option> <option value="black">Black</option> </select></p> <p> Select a font type: <select style="width: 150px;" id="fonttype" name="fonttype" /> <option value="Verdana">Verdana</option> <option value="New Times Roman">New Times Roman</option> <option value="Arial">Arial</option> </select></p> <p> Select a font size: <select style="width: 150px;" id="fontsize" name="fontsize" /> <option value="10px">10pt</option> <option value="12px">12pt</option> <option value="16px">16pt</option> <option value="20px">20pt</option> </select></p> <p> <label for="savedata">Save as a cookie?<label> <input type="checkbox" id="savedata" name="savedata" value="1" /> </p> <input type="submit" name="process" value="Process" /> </form> </body> </html> <?php if (isset($_POST['savedata'])) { setcookie("testtext", $_POST['testtext'], 0, '/', FALSE); setcookie("textcolour", $_POST['textcolour'], 0, '/', FALSE); setcookie("fonttype", $_POST['fonttype'], 0, '/', FALSE); setcookie("fontsize", $_POST['fontsize'], 0, '/', FALSE); } /* session_start(); Removed to see if the cookie is saving $_SESSION['testtext'] = $_POST['testtext']; $_SESSION['textcolour'] = $_POST['textcolour']; $_SESSION['fonttype'] = $_POST['fonttype']; $_SESSION['fontsize'] = $_POST['fontsize']; */ ?> <html> <head> <title>The Output of textinput.php</title> </head> <body> <p>The following text was entered and processed using the options you set for it:</p> <p <?php echo 'style="color:' . $_COOKIE['textcolour'] . '; ' . 'font-family:' . $_COOKIE['fonttype'] . '; ' . 'font-size:' . $_COOKIE['fontsize'] . ';"'; ?>> <?php echo $_COOKIE['testtext']; ?></p> </body> </html> Oh, and also, the values you have just set won't be in the $_COOKIE array (unless you put them there) until the next page refresh, because obviously your browser has requested the page before the cookies are set, so hasn't sent them to PHP . Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840386 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 setting cookies to expire on 0 would mean they die right away! kinda pointless setting them! Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840393 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 HOLD UP!!! I just found something a little weird.... I have removed all variables except the test text so that I only have 1 output variable. I ran the input with the word "blue" and ticked the box. All I got back was: (making all output red so you can see it) The following text was entered and processed using the options you set for it: Then I ran it again and I entered "red", ticked the box and what did I get back? The following text was entered and processed using the options you set for it: blue I thought that was odd so I ran it again and entered "green", ticked the box and got the following: The following text was entered and processed using the options you set for it: red To see if my if statement was working, I ran it again, entered "purple" and did NOT tick the box and got: The following text was entered and processed using the options you set for it: green and again with "blue" and got green returned. It IS saving the cookie.... it's just not recalling the data on the first run. I also tried to enter "blue", tick the box and it was still green but upon refreshing the page, it returned blue. Right, now that we have established that the cookies are actually saving, how do we sort out the fact that it is recalling the value before the new value was set instead of the current value. Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840394 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 Jack you beat me to it. lol Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840396 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 setting cookies to expire on 0 would mean they die right away! kinda pointless setting them! No, an expiry of 0 causes it to act like a session cookie. So it expires when the browser is closed or whatever. Can't you read chap?! Oh' date=' and also, the values you have just set won't be in the $_COOKIE array (unless you put them there) until the next page refresh, because obviously your browser has requested the page before the cookies are set, so hasn't sent them to PHP .[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840398 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 Jack you beat me to it. lol Ahh you can read Lol my apologies. Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840399 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 Right, upon entering the data and refreshing the page, all the formatting outputs as it should. The if statement is working as well and not saving data to the cookie if the checkbox is not ticked. How do I solve this dilemma though? Use Javascript to auto-refresh the page before posting the output or is there a better way of doing things? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840410 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 echo the $_POST variables rather than the $_COOKIE ones? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840415 Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 if (isset($_POST['savedata']) || isset($_COOKIE['savedata'])) { setcookie("savedata", true, 0, '/', FALSE); setcookie("testtext", $_POST['testtext'], 0, '/', FALSE); Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840426 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 MadTechie, that didn't work. jackpf, I could do that but that would defeat the point of the exercise (to get the user to store one cookie, write over it and then prove that the if statement is working by NOT writing over it with a new value and getting the old value). I just added a quick single refresh javascript to the header of the output file and it worked a charm! Here is the code for those who might ever want it. <script language="JavaScript" type="text/javascript"> var reloaded = false; var loc=""+document.location; loc = loc.indexOf("?reloaded=")!=-1?loc.substring(loc.indexOf("?reloaded=")+10,loc.length):""; loc = loc.indexOf("&")!=-1?loc.substring(0,loc.indexOf("&")):loc; reloaded = loc!=""?(loc=="true"):reloaded; function reloadOnceOnly() { if (!reloaded) window.location.replace(window.location+"?reloaded=true"); } reloadOnceOnly(); //You can call this via the body tag if desired </script> This topic is now solved! Thank you gents. You guys are the coolest! I am gonna love this place almost as much I will love learning PHP! Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840429 Share on other sites More sharing options...
cforrester Posted May 23, 2009 Author Share Posted May 23, 2009 where do I mark it as solved? Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840431 Share on other sites More sharing options...
jackpf Posted May 23, 2009 Share Posted May 23, 2009 PHP is well sick. At the bottom. Quote Link to comment https://forums.phpfreaks.com/topic/159324-solved-why-are-my-cookies-not-working/#findComment-840434 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.