-4

When running my PHP script, I'm getting following error:

Warning: Cannot modify header information - headers already sent by (output started at /wara/index.php:12) in /wara/index.php on line 18

I am calling header() and setCookies() methods.

Any syggestion?

  • what do you have in index.php row 12? – Lucabro Aug 09 '13 at 08:16
  • Have you made sure there's no whitespace, HTML or `echo`s before those calls? – MDEV Aug 09 '13 at 08:16
  • Also ensure that no code prior to those calls hasn't errored, as that could also cause this error – MDEV Aug 09 '13 at 08:17
  • 1
    Lokes, you may want to show us your script, so we may be able to help you better...though I have a pretty good idea as to what's wrong from that error message alone, it's always good practice to give us something to work with. – ATaylor Aug 09 '13 at 08:22

3 Answers3

4

Yeah, check with your favourite text editor, if your file encoding is UTF-8 with BOM. If it is, convert it to 'UTF-8' without BOM and see if it does the trick.

As for the why:

BOM - ByteOrderMask is an 'invisible' three-character string, which is embedded at the very beginning of your script. Before anything else, the BOM is written. And since it isn't within the PHP tags, it is also sent to the client, which is why you're getting the 'headers already sent' error when trying to send out your own headers.

PHP can't handle UTF-8 files with BOM to my knowledge and it really isn't necessary either, since UTF8 itself has the Endianess problem covered. (Unlike UTF16 for example)

If you're unsure and your editor doesn't tell you either, feel free to open your source file in a hex editor and look at the first three characters. If they aren't the ascii representation of '

Edit: As pointed out in the comment section, BOM is represented as the hexadecimal values EF BB BF (in this order) for UTF-8. For an UTF-8 incapable editor, this would appear as . For more information, you may want to consult this Wikipedia article

ATaylor
  • 2,598
  • 2
  • 17
  • 25
2

It says: "output started at /wara/index.php:12". Check your /wara/index.php on line 12 (maybe an "echo" or a print)

From manual (http://www.php.net/manual/en/function.header.php):

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
0

On line 12 you have an output like echo, print or just an html tag notation outside of php code. You cannot modify header information with header() after you output something because you already sent a header.