note 32746 deleted from function.headers-sent by nlopess
| From: | nlopess@php.net | Date: | Tue, 16 Dec 2003 15:22:20 +0000 |
| Subject: | note 32746 deleted from function.headers-sent by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62066@lists.php.net to get a copy of this message | ||
Note Submitter: dougday@qwest.net
----
It would be well to note that determining whether or not the headers have been sent is best used
when creating very complex .php files. For the majority of us, we can simply just call the headers
at the top of our php files like so:
<?php
if (strlen($_SERVER['PHP_AUTH_USER']) <= 0)
header("WWW-Authenticate: Basic realm=\"WallyWorld\"");
else
echo "Welcome {$PHP_AUTH_USER}!";
?>
But, when your php code uses a lot of included files that are combined together for the final
result, it can be quite useful to find out whether or not the headers have been sent or not to see
if you can keep going. (For instance, if an error message was displayed in one of the .php files
included, then you could not modify the headers in another script) For example:
<?php
include_once('/include/auth.php');
include_once('/include/browser.php');
// force the page to be refreshed, not loaded from the cache
if (!headers_sent())
header("Pragma: no-cache");
?>
Probably the most useful aspect of the headers_sent function is within the include files themselves
(in this case auth.php and browser.php). If during the include process one of the files displays an
error message (or other message), then if another included file tried to modify the headers, it
would fail. Since the order you include your files may change as well, it can be safer when
manipulating header information to use headers_sent() to make sure that you *can* before you try.
Regards,
Doug