0

Warning: Cannot modify header information - headers already sent by (output started at example.php:1)

It looks like there is an unlimited amount of these questions and they keep coming. I agree that in most cases the answer is out there already.

I ran into the same issue myself. There was a mysterious character in the output that prevented me from setting the headers. Since it was a big application that has a lot of files, going through each one to search for it was not an option.

I believe the issue is due to a BOM or an extra space before the <?php opening tag somewhere in one of the included files. When I open the file referred to in the error message it looks like there's nothing wrong with it.

How do I find where the issue is?

Community
  • 1
  • 1
ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • You start by reading the error message, because it TELLS you where the problem is: example.php, line #1. – Marc B Jan 27 '15 at 14:08
  • You end by reading the question, because it tells you `When I open the file referred to in the error message it looks like there's nothing wrong with it` – ᴍᴇʜᴏᴠ Jan 27 '15 at 14:10
  • if it's line #1, then it's whitespace before your opening `` tag sets is considered output. Remember: There's no such thing as a PHP script. There's only files which have PHP code blocks within them. Anything outside the code blocks is output. – Marc B Jan 27 '15 at 14:12
  • Thanks. I presumed it could've been any other included or parent file. I just shared my experience in solving this because I couldn't find an existing solution. Is there anything wrong with that? – ᴍᴇʜᴏᴠ Jan 27 '15 at 14:14

1 Answers1

0

Here's the automated solution that has helped me to track down that character. SSH access required.

grep -v -R "^<?php" /home/public_html/ --include \*.php | grep "<?php"

grep is a very useful tool that is able to recursively search through files and folders contents.

  • -v means exclude, i.e. output the opposite of whatever matches the regular expression I'll pass later
  • -R means recursively, i.e. search inside the folder provided and inside it's subdirectories
  • "^<?php" is the expression that matches the lines that begin (^) precicely with the PHP opening tag. Again, -v is used to invert this so I get the lines that do not - which is what I'm after.
  • /home/public_html/ is the folder with all of my website files
  • --include \*.php we're searching inside the .php files only
  • grep "<?php" is a trick that captures the first lines of the file only

Thing is, that the -v flag inverts the search results and outputs whatever doesn't match the expression. So if a file has 1000 lines with only first one being the <?php opening tag, that'll output all 999 because thouse do not match the regexp. Since I need to look at the first line only, I'm forwarding the greps output into another grep that filters the results one more time and makes sure I get the lines that have another condition I pass - which is again, the opening tag. This is a workaround which this way limits greps output to the first line only.

Hope this helps

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57