How can I find out where my output started?
I noticed that there are two empty lines above the HTML in the source code of my website. It looks like this:
<!-- Two empty lines here, StackOverflow won't let me post empty lines -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
The HTML is generated using PHP. How can I find out where the output started?
I t开发者_Python百科ried adding <?php header('...'); ?>
after <html>
, hoping to trigger an error like "Cannot modify header. Output started at line ..." But no error occurred.
If you want to obtain the filename and line-number where the output started, you can call the headers_sent()
function with two optional parameters:
$sent = headers_sent($file, $line);
Those two variables $file
and $line
will contain the information you're looking for after the function has been invoked. You don't need to wait for the error message (but you might need to flush output (at many places) if you don't see any error message and you want to pinpoint the origin). So ensure that output buffering is disabled.
It probably doesn't trigger an error because what you've printed so far it too short and was still buffered before your header call.
Try outputting some more data, for example:
echo str_repeat('a', 10000);
And then try to output some header. You should then see the expected PHP error (assuming your error reporting settings are tuned properly).
To answer you're question directly, any echo, or print statement, or any function that returns results directly to the standard output will cause output to start. Also if you have any HTML or text or blank lines before an opening <?php
tag this will cause output to start.
You can fix this directly by adding a ob_start() at the top of your entry document just below a <?php
tag. And adding a ob_end_flush() at the very end of your document. That would look like this.
Add this to the very top of your document:
<?php
ob_start();
?>
Add this to the very bottom of your document:
<?php
echo ob_end_flush();
?>
This allows you to keep the current code that you do have, and output headers where ever you need them within the code without getting that error.
Display errors by putting these lines somewhere in your PHP:
error_reporting(E_ALL);
ini_set('display_errors','on');
This will trigger that header error!
It's probably at the end of the PHP file; you should consider removing the ending PHP tag (?>
) to eliminate this problem.
I've got the same problem on Drupal (lots of iles), I try several of the above answers without any results.
Finally I found the find way (cf. Elegant way to search for UTF-8 files with BOM?), it points out a dumb mistake : spaces at the beggining of a module file :
find . -type f -name '*.module' -print0 | xargs -0r awk '/^ / {print FILENAME}{nextfile}'
find . -type f -name '*.php' -print0 | xargs -0r awk '/^ / {print FILENAME}{nextfile}'
find . -type f -name '*.inc' -print0 | xargs -0r awk '/^ / {print FILENAME}{nextfile}'
I just had a similar problem with hidden BOMs, but this applies to blanks lines and other spurious characters as well, so I thought to add here my findings.
Adding a fake header(""); call in the code is the best way to find where the output started, but... most likely, the PHP configuration has output_buffering enabled by default. Check this out.
By adding this at the beginning of your script:
if (ob_get_level()) ob_end_clean();
will turn off output buffering and make the header() call complain and say where the output started.
This can occur if you have any empty lines above the very first <?php
in your code (assuming you have it at the 'top' of you file)
精彩评论