note 61695 deleted from function.setcookie by betz
| From: | betz@php.net | Date: | Thu, 09 Feb 2006 22:16:23 +0000 |
| Subject: | note 61695 deleted from function.setcookie by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-103389@lists.php.net to get a copy of this message | ||
Note Submitter: shawn at ellev dot com
----
When setting a cookie be sure to have everything on the page inside one opening and closing
"<?php ~ ?>" tag. I'm guessing that you could also have multiple open/closing
php tags if they have no space between them (i.e. <?php ~ ?><?php ~ ?>) where the
">" and "<" are touching each other.
I had code grouped at the top of the page with multiple open/close php tags that I'm guessing
was causing white space to be passed back in a header to the browser before the cookie header was
sent. This resulted in an error like the following:
"Warning: Cannot modify header information - headers already sent by (output started at
/blah/blah/blah.php:3) in /blah/blah/blah.php on line 5"
In this example the blank space that is outside of the <?php ~ ?> tags is on line:2 (error is
occuring on line:3 but the white space is actually on line:2) and the cookie is attempting to be set
on line:5.
This is what the code might look like that would cause this error:
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
<?php require lib.php;?>
<?php $cookie_data = "1234";
setcookie('cookie_name',$cookie_data);
?>
To fix this problem you would change the code above to:
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
<?php require lib.php;
$cookie_data = "1234";
setcookie('cookie_name',$cookie_data);
?>
Note: in this example the file "lib.php" is included at load time and if it contains
multiple php tags with any white space between them it will cause a similar error, so don't
forget to check any files that your loading.
Hope this helps.