note 53578 deleted from function.header by bjori

From: Date: Mon, 27 Mar 2006 09:49:46 +0000
Subject: note 53578 deleted from function.header by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106724@lists.php.net to get a copy of this message
Note Submitter: jsun at dentistat dot com ---- If you got this message: "Warning: Cannot modify header information - headers already sent by ...." Few notes based on the following user posts: 1. Blank lines: Make sure no blank line after <?php ... ?> of the calling php script. 2. Use exit statement: Use exit after header statement seems to help some people header ("Location: xxx"); exit(); 3. PHP has this annoying problem, if your HTML goes before any PHP code or any header modification before redirecting to certain page, it'll said "Warning: Cannot modify header information - headers already sent by ...." Basically anytime you output to browser, the header is set and cannot be modified. So two ways to get around the problem: 3a. Use Javascript: <? echo "<script> self.location(\"file.php\");</script>"; ?> Since it's a script, it won't modify the header until execution of Javascript. 3b. Use output buffering: <?php ob_start(); ?> ... HTML codes ... <?php ... PHP codes ... header ("Location: ...."); ob_end_flush(); ?> This will save the output buffer on server and not output to browser yet, which means you can modify the header all you want until the ob_end_flush() statement. This method is cleaner than the Javascript since Javascript method assumes the browser has Javascript turn on. However, there are overhead to store output buffer on server before output, but with modern hardware I would imagine it won't be that big of deal. Javascript solution would be better if you know for sure your user has Javascript turn on on their browser.

« previous php.notes (#106724) next »