note 61695 added to function.setcookie

From: Date: Thu, 09 Feb 2006 20:18:23 +0000
Subject: note 61695 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-103385@lists.php.net to get a copy of this message
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. ---- Server IP: 216.194.113.175 Probable Submitter: 24.211.33.237 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61695 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61695&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61695&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103385) next »