Jump to content

[SOLVED] Can't set a cookie Please help


jmr3460

Recommended Posts

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

"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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.