Thread: setcookie
View Single Post

  #2 (permalink)  
Old 03-12-2008
Hiep Nguyen
 
Posts: n/a
Default Re: [PHP] setcookie

On Wed, 12 Mar 2008, Tim Daff wrote:

> Hi,
>
> I am learning PHP, I am trying to set a simple cookie:
>
> <html>
> <head>
> <title>Cookies</title>
> </head>
> <body>
> <?php setcookie('test', 45,
> time()+(60*60*24*7)); ?>
> </body>
> </html>
>
> Firefox is returning this error:
>
> Warning: Cannot modify header information - headers already sent by (output
> started at /Users/Daff/Sites/php_sandbox/cookies.php:7) in
> /Users/Daff/Sites/php_sandbox/cookies.php on line 7
>
> I have googled this and can't find out what I am doing wrong. Any help you
> could give me would be much appreciated.
>
> Tim


setcookie from php.net states the following:

setcookie() defines a cookie to be sent along with the rest of the HTTP
headers. Like other headers, cookies must be sent before any output from
your script (this is a protocol restriction). This requires that you place
calls to this function prior to any output, including <html> and <head>
tags as well as any whitespace

call setcookie before you output ANY THING, even error

hope that helps.
t. hiep
Reply With Quote