List Info

Thread: note 61695 deleted from function.setcookie by betz




note 61695 deleted from function.setcookie by betz
user name
2006-02-09 22:16:23
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.

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )