Skip to content

How to return http statuscodes in php?

An answer to this question on Stack Overflow.

Question

Possible Duplicate:
return a specific http status code with php

How can I programmatically send specific http statuscodes with php? Is there any Method or even a class for this?

Answer

Send a [header()][1] before you send anything else to the client. Some possible status headers follow:

header('HTTP/1.1 200 OK');
 
// Page was not found:
header('HTTP/1.1 404 Not Found');
 
// Access forbidden:
header('HTTP/1.1 403 Forbidden');
 
// The page moved permanently should be used for
// all redrictions, because search engines know
// what's going on and can easily update their urls.
header('HTTP/1.1 301 Moved Permanently');
 
// Server error
header('HTTP/1.1 500 Internal Server Error');
  
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');

[1]: http://php.net/manual/en/function.header.php