jmr3460 Posted April 18, 2009 Share Posted April 18, 2009 I have gone to php.net for help and when I use their code to set some cookies I get a warning: Warning: Cannot modify header information - headers already sent by (output started at /home3/simplic5/public_html/contact/login/cookie.php:1) in /home3/simplic5/public_html/contact/login/cookie.php on line 2 This is the code: <?php setcookie("cookie[three]","cookiethree"); if(isset($_COKKIE['cookie'])){ foreach ($_COOKIE['cookie'] as $name => $value) { echo "$name : $value"; } } ?> Can anyone help me? Link to comment https://forums.phpfreaks.com/topic/154655-solved-cant-set-a-cookie-please-help/ Share on other sites More sharing options...
soak Posted April 18, 2009 Share Posted April 18, 2009 "headers already sent" means you've already started displaying your page. You need to call setcookie before anything has been displayed to the user at all. If it isn't possible to move the cookie script up then you could use ob_start() at the start of your code to buffer anything being displayed. Link to comment https://forums.phpfreaks.com/topic/154655-solved-cant-set-a-cookie-please-help/#findComment-813268 Share on other sites More sharing options...
bloodgoat Posted April 18, 2009 Share Posted April 18, 2009 I have gone to php.net for help and when I use their code to set some cookies I get a warning: Warning: Cannot modify header information - headers already sent by (output started at /home3/simplic5/public_html/contact/login/cookie.php:1) in /home3/simplic5/public_html/contact/login/cookie.php on line 2 This is the code: if(isset($_COKKIE['cookie'])){ Can anyone help me? If you just copied and pasted your code, one thing: on your call for isset() you misspelled cookie, as in the excerpt. I mean, I doubt it fixes your problem, but just a heads-up. Link to comment https://forums.phpfreaks.com/topic/154655-solved-cant-set-a-cookie-please-help/#findComment-813400 Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 Thanks for the heads up I need to start rereading my code. I was not allowed to copy and paste from php.net. I finally got my script to set a cookie though. I moved the setcookie() to the very top of the page, although I think that this will set my cookie too early. I was reading and I read that the cookie needs to be set before any output has started. Is the !isset statement counted as being before or after output starts. I will do a little experimenting with that. Link to comment https://forums.phpfreaks.com/topic/154655-solved-cant-set-a-cookie-please-help/#findComment-813408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.