0

So I'm trying to change the headers in PHP with this code:

header("HTTP/1.0 404 Not Found");

And I get the following error message:

Cannot modify header information - headers already sent by (output started at path\index.php:1)

So I've read about this error, and I found out that it happens because an output is already sent before I'm trying to change the headers. Problem is it tells me that output is sent in index.php on line 1, yet line 1 on that file is:

<?php

I've checked and there is no whitespace characters before the <. I even tried to change the first line of the file to:

<?php header("HTTP/1.0 404 Not Found");

Yet I still get the same error.

Why does this happen and how can I solve it?

Cokegod
  • 8,256
  • 10
  • 29
  • 47

2 Answers2

0

I could be BOM related. This is related to the editor you use and which encoding you save in. Please check UTF-8 BOM signature in PHP files

EDIT:

Actully check this answer instead. How to fix "Headers already sent" error in PHP .. its great.

Community
  • 1
  • 1
Ronni Skansing
  • 1,494
  • 1
  • 17
  • 33
  • So I've looked in the answer you linked to, and there it says that `session_start()` also modifies the header information. Thing is `session_start()` is working flawlessly, while `header()` is showing this error. – Cokegod Aug 07 '13 at 05:29
  • By the way if this wasn't clear, this answer wasn't helpful. I have no whitespace character before the ` – Cokegod Aug 07 '13 at 05:56
  • Did you read through the second link? So if you make a new file, in a new project, insert the header, and read it. Do you get the same error? – Ronni Skansing Aug 08 '13 at 10:26
0

I don't know for sure what caused this problem, but eventually after I changed 'php.ini' (to install PDO) and restared the server I managed to change headers via PHP successfully.

Cokegod
  • 8,256
  • 10
  • 29
  • 47