pnrule Posted February 22, 2009 Share Posted February 22, 2009 EDITED BY WILDTEEN88 RESTORED. Deleted wrong post I am trying to make a website where certain divs are only displayed if a cookie says they should be. If the cookie is not present, then it will display it by default. This is the PHP code of the page: <?php if(isset($_COOKIE["1"])) { if($COOKIE["1"] = 1) { include("1.html"); } } else { include("1.html"); } if(isset($_COOKIE["2"])) { if($COOKIE["2"] = 1) { include("2.html"); } } else { include("2.html"); } if(isset($_COOKIE["3"])) { if($COOKIE["3"] = 1) { include("3.html"); } } else { include("3.html"); } if(isset($_COOKIE["4"])) { if($COOKIE["4"] = 1) { include("4.html"); } } else { include("4.html"); } if(isset($_COOKIE["5"])) { if($COOKIE["5"] = 1) { include("5.html"); } } else { include("5.html"); } include("pref.html"); ?> On the "pref.html", I have a form with 5 checkboxes. <div class="c"> <form action="preferences.php" method="post"> <fieldset> <legend>save prefs</legend> which should you see?<br /> <input type="checkbox" name="1" id="1" value="1"><label for="1" />facebook</label><br /> <input type="checkbox" name="3" id="3" value="3"><label for="3" />gmail</label><br /> <input type="checkbox" name="2" id="2" value="2"><label for="2" />myspace</label><br /> <input type="checkbox" name="4" id="4" value="4"><label for="4" />tinypic</label><br /> <input type="checkbox" name="5" id="5" value="5"><label for="5" />weather</label><br /> <input type="submit" value="save" class="button" /> <input type="reset" value="clear" class="button" /> </fieldset> </form> </div> Then, when you submit, I have this page running to store the cookies and troubleshoot. <?php $expire=time()+60*60*24*30; setcookie("1", $POST["1"], $expire); setcookie("2", $POST["2"], $expire); setcookie("3", $POST["3"], $expire); setcookie("4", $POST["4"], $expire); setcookie("5", $POST["5"], $expire); ?> <html> <head> <meta http-equiv="REFRESH" content="10;url=login.php"> </head> <body> Preferences Saved<br /> <?php echo "Expire Time: " . $expire . "<br />"; echo "Cookie1 Val: " . $_COOKIE["1"] . "<br />"; ?> </body> </html> For some reason, whenever I submit the form, nothing gets stored to a cookie. It doesn't display anything when I echo the cookie value, and the boxes still show up on the main page. I am testing on WAMPserver. Is it a problem with WAMP, or an error in my code? Anyone? How do I fix this? Also, problem #2, which isn't nearly as important right now: How do I go about saving sever usernames and passwords to a cookie and having them filled in when a user views the page? Quote Link to comment Share on other sites More sharing options...
only one Posted February 22, 2009 Share Posted February 22, 2009 $POST["1"] should be $_POST["1"] setcookie("1", $_POST["1"], $expire); Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 22, 2009 Share Posted February 22, 2009 You cannot retrieve cookie values as soon as they are set. You must refresh the page in order for PHP to retrieve the new cookie values. This is to do with how the HTTP protocol works. Quote Link to comment Share on other sites More sharing options...
pnrule Posted February 22, 2009 Author Share Posted February 22, 2009 Another question: Am I allowed to do this? if(isset($_COOKIE["1" || "2" || "3" || "4" || "5"])) { if($COOKIE["1"] = 1) { include("1.html"); } } else { include("1.html"); The idea being if any of the cookies are set at all, it will only display this particular piece if it's cookie is set to 1? I'm not sure if this is how I should to do it. I need it to check if any cookies have been set, if they have and = 1, then display the html, but if that particular cookie isn't set or = 0 then it should not display the html. And, if no cookies are set at all, then display it. Quote Link to comment Share on other sites More sharing options...
only one Posted February 22, 2009 Share Posted February 22, 2009 Well you could. if(isset($_COOKIE["1"]) || isset($_COOKIE["2"]) || isset($_COOKIE["3"]) || isset($_COOKIE["4"]) || isset($_COOKIE["5"]) ) { if($_COOKIE["1"] = 1) { include("1.html"); } } else { include("1.html"); } But then it would be much easier to loop trhough them. foreach( $_COOKIE as $cookie ) { if( empty($cookie) ) { $empty = true; } } if( empty($empty) ) { include("1.html"); } Quote Link to comment 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.