setcookie

This is a discussion on setcookie within the PHP General forums, part of the PHP Programming Forums category; Hi, I am learning PHP, I am trying to set a simple cookie: <html> <head> <...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-12-2008
Tim Daff
 
Posts: n/a
Default setcookie

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
Reply With Quote
  #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
  #3 (permalink)  
Old 03-12-2008
Zareef Ahmed
 
Posts: n/a
Default Re: [PHP] setcookie

As a dirty trick you can put following line on the top of your script,
it will work
ob_start();

But you should try to know why it is not working, and what exactly
ob_start will impact your application and What is the thing called
"Output Buffering".

Zareef Ahmed

On 3/12/08, Hiep Nguyen <hiep@ee.ucr.edu> wrote:
>
> 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
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Zareef Ahmed
http://www.zareef.net
A PHP Developer in India

Reply With Quote
  #4 (permalink)  
Old 03-12-2008
Richard Heyes
 
Posts: n/a
Default Re: [PHP] setcookie

> 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


You must use set cookie() before you send any output to the browser,
including whitespace.

--
Richard Heyes
Employ me:
http://www.phpguru.org/cv
Reply With Quote
  #5 (permalink)  
Old 03-12-2008
Wolf
 
Posts: n/a
Default Re: [PHP] setcookie



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


<?php setcookie('test', 45, time()+(60*60*24*7)); ?>
<html>
<head>
<title>Cookies</title>
</head>
<body>



</body>
</html>

Cookies HAVE to be at the top of the page... You output ANYTHING else
and you get the error you wound up getting.

Wolf

Reply With Quote
  #6 (permalink)  
Old 03-12-2008
Ray Hauge
 
Posts: n/a
Default Re: [PHP] setcookie

Wolf wrote:
>
>
> 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

>
> <?php setcookie('test', 45, time()+(60*60*24*7)); ?>
> <html>
> <head>
> <title>Cookies</title>
> </head>
> <body>
>
>
>
> </body>
> </html>
>
> Cookies HAVE to be at the top of the page... You output ANYTHING else
> and you get the error you wound up getting.
>
> Wolf
>
>


If you're not sure if data has already been sent to the client (I ran
into an included file having a space after >?) you can use
http://us3.php.net/manual/en/function.headers-sent.php before you call
setcookie();

If nothing else it'll help with diagnosing this error when you run into
it again.

--
Ray Hauge
www.primateapplications.com
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:23 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0