-2

Possible Duplicate:
“Warning: Headers already sent” in PHP

Hi there I'm cannot get 404 status code for pages that aren't in my site. My .htaccess looks like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

And so all my pages go to index.php from there they go to bootstrap and there it's send to and error controller. In the error View is make a:

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

but it give me an error that "Cannot modify header information - headers already sent by". Now i do get a 200 Ok status if i delete this line. I think this is because i send everything to index.php and that status is 200 Ok. So how can make the difference for those files from .htaccess? Or do you have another way to deal with this? Thanks

Community
  • 1
  • 1
Pop Radu
  • 17
  • 5

2 Answers2

1

Probably you are sending HTTP header in wrong place.

If you are using an MVC PHP framework, you need to read documentation of your framework to totally undersand dispatching, routing and request handling processes. Answer is depends by framework. There must be an information about handling requests and sending headers.

If not, so that index.php and bootstrap file is your own, you need to try to send 404 header in bootstrap file or controller level (error controller?), not in a view file. View layer is a bad place to send headers in a MVC structure.

Also don't forget to sure you are not sending any output to client before sending headers.

edigu
  • 9,878
  • 5
  • 57
  • 80
  • hy i try to put in the index.php in the bootstrap or in errorController but no good result. I've made my mvc after this tutorial series http://www.youtube.com/watch?v=Aw28-krO7ZM. i've post a question there to but now anwser. – Pop Radu Nov 04 '12 at 23:00
1

In addition to foozy's answer, check your PHP code for any whitespace (spaces, tabs, newlines etc.) before your opening <?php tags. If you find any, delete it.

If there's any whitespace, this will cause the "Cannot modify header information" error.

Another useful way to debug this is to put an exit('stopped'); right before header("HTTP/1.1 404 Not Found");. When you refresh the page and view the source, ideally you should see stopped as the very first thing printed (with NO spacing in front). If spacing exists, there's the problem.

Wireblue
  • 1,329
  • 1
  • 14
  • 24