No puedo registrar la cookie, recibí un código de error sobre los encabezados [duplicado]

I have the following code. It works perfectly on a Localhost:

if(isset($_GET['vote']) AND isset($_GET['ididea']))
   setcookie($_GET['ididea'], $_GET['ididea'], time() + 365*24*3600, null, null, false, true);

And this is the error by the server:

Warning: Cannot modify header information - headers already sent by (output started at /home/sirobdco/public_html/index.php:15) in /home/sirobdco/public_html/includes/votetovote/includes/2/bodyvotetovote.php on line 4

And what I have on line 15?The first line of code in body:


preguntado el 25 de septiembre de 13 a las 19:09

What's on lines 1-14 of that file? -

it seems like your included file bodyvotetovote.php already makes some echo's or other output. -

Si tienes <?php in 15 line, you've also got many other chars in previous line. That's the problem. -

Yes guys bodyvotevote.php is include (using php) in index.php. Thats the problem what can i do to fix it? Thank you. -

1 Respuestas

You cannot set cookies after output is started. You must put the cookie section at the top of the script (before any output is done) or you must use ob_start() to buffer the output.

Respondido el 25 de Septiembre de 13 a las 19:09

Where i should use this? Before what? Thank you so much. - user2632603

Assuming since the error is on line 15, that there is output being done before line 15. Output is any text that would display to the user. Even a <!DOCTYPE html> tag would be enough to send headers. Anything dealing with cookies should happen before ANYTHING that could output to a user. Also nbd, hope this helps :) - skrilled

Oh i got is working!! Thank you - user2632603

@user2632603 - if this answer helped you, please accept it as correct - Andrewsi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.