clown[NOR] Posted April 6, 2007 Share Posted April 6, 2007 Hi! This is my first time here on your forum, and it looks like a good PHP community. I'm gonna go straight to the case... I've made a shoutbox, and now i'm trying to add cookies to it so the suer don't have to write their name and www address everytime they want to leave a shout.. but the setcookie() wont work. I've used the following to double check the cookie <?php if (isset($_COOKIE['sbname'])) { echo $_COOKIE['sbname']; } else { echo "Could not find any cookies matching that name!"; } ?> <br /> <?php if (isset($_COOKIE['sburl'])) { echo $_COOKIE['sburl']; } else { echo "Could not find any cookies matching that name!"; } ?> but all it returns is "Could not find....." The following is the exact code I use to add the shouts: (only change is the filename) <?php setcookie("sbname", $_REQUEST['sbname'], time()+31536000); setcookie("sburl", $_REQUEST['sburl'], time()+31536000); $phps_sbname = $_REQUEST['sbname']; $phps_sburl = $_REQUEST['sburl']; $phps_sbmessage = $_REQUEST['sbmsg']; if ($phps_sburl == '') { $phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong>" . $phps_sbname . ":</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>"; } elseif ($phps_sburl != '') { $phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong><a href=" . $phps_sburl . " target=_blank>" . $phps_sbname . "</a>:</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>"; } #echo "Name: " . $phps_sbname . "<br>URL: " . $phps_sburl . "<br>Message: " . $phps_sbmessage . ""; $phps_sbfh = fopen("filename", "ab"); fwrite($phps_sbfh,$phps_newshout); fclose($phps_sbfh); Header("Location: http://www.nstclan.com/phpschool/v1.1/index.php"); ?> Anyone having any ideas that might help me out here? Regards, Clown[NOR] Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/ Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 www.php.net/setcookie I would look into the full parameters of set cookie. But either way your code is flawed. You need the setcookie to be inside of an if statement: IE: <?php // if we have a value set it. if (isset($_REQUEST['sbname'])) { if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) { // unset previous cookie setcookie("sbname", '', time()-31536000); setcookie("sburl", '', time()-31536000); } // set new cookie setcookie("sbname", $_REQUEST['sbname'], time()+31536000); setcookie("sburl", $_REQUEST['sburl'], time()+31536000); } ?> What was happening was you were setting the cookie with an empty value and therefore it was not "set". Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-222976 Share on other sites More sharing options...
clown[NOR] Posted April 6, 2007 Author Share Posted April 6, 2007 I've tried many different ways now, but still no luck.. just can't figure out wth I'm doing wrong.. And it's probably not that werid that I cant see it... I've only been working with more advanced php than include() for about 3 days now... But my code looks like this atm: <?php $phps_sbname = $_REQUEST['sbname']; $phps_sburl = $_REQUEST['sburl']; $phps_sbmessage = $_REQUEST['sbmsg']; if ($phps_sbname == '') { echo 'Please enter a name'; } elseif ($phps_sbmessage == '') { echo 'Please enter a message'; } else { if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) { setcookie("sbname", $_REQUEST['sbname'], time()-31536000); setcookie("sburl", $_REQUEST['sburl'], time()-31536000); } setcookie("sbname", $_REQUEST['sbname'], time()+31536000); setcookie("sburl", $_REQUEST['sburl'], time()+31536000); if ($phps_sburl == '') { $phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong>" . $phps_sbname . ":</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>"; } elseif ($phps_sburl != '') { $phps_newshout = "\n<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td bgcolor=#FFFFFF><strong><a href=" . $phps_sburl . " target=_blank>" . $phps_sbname . "</a>:</strong> " . $phps_sbmessage . "</td></tr><tr><td bgcolor=#999999 height=1></td></tr></table>"; } $phps_sbfh = fopen("filename.txt", "ab"); fwrite($phps_sbfh,$phps_newshout); fclose($phps_sbfh); Header("Location: http://www.nstclan.com/phpschool/v1.1/index.php"); } ?> Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223133 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 You are still always reseting the cookie, you only want to set the cookie if it is necessary. Think through that logic, how can I only set the cookie if it is necessary??? Than you will have the answer to your question. <?php // if we have a value set it. if (isset($_REQUEST['sbname'])) { if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) { // unset previous cookie setcookie("sbname", '', time()-31536000); setcookie("sburl", '', time()-31536000); unset($_COOKIE['sbname']); unset($_COOKIE['sburl']); } if (!isset($_COOKIE['sbname'])) // set new cookie setcookie("sbname", $_REQUEST['sbname'], time()+31536000); setcookie("sburl", $_REQUEST['sburl'], time()+31536000); } } ?> Maybe that logic will help you. Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223160 Share on other sites More sharing options...
clown[NOR] Posted April 6, 2007 Author Share Posted April 6, 2007 still the same... I just dont understand why it's so hard creating these cookies.. I've used cookies for login and for a couple of tutorials I've made (pageload counter and last visit)... and havent had any problems at all... but now it just won't even create the cookies... Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223180 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 Are you sure that section of code is even getting processed? Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223182 Share on other sites More sharing options...
clown[NOR] Posted April 6, 2007 Author Share Posted April 6, 2007 well... heh.. that would be a negative... i dont know how to check that =) but I have no reason to belive that it dont get processed since it's added before the code that writes the info to the shout file... and the shouts appears on the site as they should be... and in my logical thinking that means that it has to process the cookie section before it can continue on to the "add shout" part of the code.. Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223186 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 <?php // if we have a value set it. if (isset($_REQUEST['sbname'])) { if (isset($_COOKIE['sbname']) && $_COOKIE['sbname'] != $_REQUEST['sbname']) { // unset previous cookie // setcookie("sbname", '', time()-31536000); // setcookie("sburl", '', time()-31536000); //unset($_COOKIE['sbname']); // unset($_COOKIE['sburl']); print "cookie would be unset"; } if (!isset($_COOKIE['sbname'])) // set new cookie // setcookie("sbname", $_REQUEST['sbname'], time()+31536000); // setcookie("sburl", $_REQUEST['sburl'], time()+31536000); print "new cookie would have been set"; } } ?> Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223187 Share on other sites More sharing options...
clown[NOR] Posted April 6, 2007 Author Share Posted April 6, 2007 thanks... yeah I added it to the code, and removed the Header at the bottom of the site, so I could check for the messages.. but nothing came up... just a white blank page with no text at all... so I guess it doesn't get processed... Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223189 Share on other sites More sharing options...
clown[NOR] Posted April 6, 2007 Author Share Posted April 6, 2007 i don't think my shout.php file like the cookies it's like the file just ignores it like it's not cool enough to be a part of the shout =) hehe... gotta admit that I'm scratching my head really intense here now =) Link to comment https://forums.phpfreaks.com/topic/45900-setcookie-doing-its-job/#findComment-223201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.