setcookie causes "Header already sent?"

No matter where i put th cookie code in the header i get an erro “Header already sent” and the error code points to my cookie code, so i’m not sure what is actually getting “sent” before the cookie if it’s causing the error :stuck_out_tongue:
The code i’m trying to add to my header is-
[php]<?php $value = $_GET['r']; setcookie ('r', $value, time()+60 * 60 * 24 * 30);?>[/php]
is this right and will it work?
I have tried the standard steps to fix the error which makes me think it might be something with my code.
Thanks to anyone who can help

Well, that code might work. First, you have to know what is inside of “r”. This could be hacked as you
are passing it as an argument. So, anything could be passed as your cookie name.

Next, cookies can not be set just anywhere in your code. They usually are set before you send anything
to the browser. So, if you display ANY code or text or webpage HTML before setting the cookie it will not
work. In PHP which is handled SERVER-SIDE, the cookie is set there. So, it is done before the browser
sees any code. So, in that case, you need to place that line BEFORE HTML. Then, PHP sets the cookie
and continues on with the code and the browser sees whatever you are sending to it.

The header error is because you either placed the PHP code after the HTML and BODY code and that means
the browser already has grabbed the headers from the page. ( Even one space sent out is a header! )

Here is a link that shows you a sample on how to do it… Hope it helps…
http://www.w3schools.com/php/func_http_setcookie.asp

Sponsor our Newsletter | Privacy Policy | Terms of Service