Modify HTTP Headers (Examples)

Many examples that show how to use the header() function of PHP

If you want to check your headers, you can use web based tools like:,
web-browser extensions (e.g. LiveHTTPHeaders, ieHTTPHeaders) or another third-party software tool.

// See related links for more status codes
// Use this header instruction to fix 404 headers
// produced by url rewriting...
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');
// Redirect to a new location:
// Redriect with a delay:
header('Refresh: 10; url=');
print 'You will be redirected in 10 seconds';
// you can also use the HTML syntax:
// <meta http-equiv="refresh" content="10; />
// override X-Powered-By value
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');
// content language (en = English)
header('Content-language: en');
// last modified (good for caching)
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');
// set content length (good for caching):
header('Content-Length: 1234');
// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=""'); 
header('Content-Transfer-Encoding: binary');
// load the file to send:
// Disable caching of the current document:
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');
// set content type:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // plain text file
header('Content-Type: image/jpeg'); // JPG picture
header('Content-Type: application/zip'); // ZIP file
header('Content-Type: application/pdf'); // PDF file
header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
header('Content-Type: application/x-shockwave-flash'); // Flash animation
// show sign in box
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
Snippet Details

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!

Older comments:

shlaing June 21, 2011 at 06:27
Oh! Extremely helpful for me and others, thanks "Jonas John
archana May 09, 2011 at 07:13
Mahesh April 25, 2011 at 17:57
Information is very useful...
none March 28, 2011 at 05:59
I'm creating my first site as my project,and i have a hardtime in making a comment,'cause everytime I refresh the page,the previous comment will also resend,eventhough i did not write anything on the comment box.
noe November 24, 2010 at 19:26
How to change value of header("Server:") ???
It's value is Apache how to change it to a custom Server Name?? like: Banana or Google
Charnold October 18, 2010 at 19:01
header("HTTP/1.0 410 Gone");
The requested page has been removed.
Alexander September 23, 2010 at 21:36
Thanks for all the great examples in one place!
Simranjit singh April 22, 2010 at 09:02
Hello i really appriciate you. Thanks for providing such a great informtion!!
Danko - April 10, 2010 at 15:15
Great list! Thanks
However, can you explain where could I see these header statement? I used Firebug with Google PageSpeed, but nothing appears whether cache expiration or pragma
tresloukadu December 07, 2008 at 14:45
Sometimes depending on the situation i use to put some html code too. Here is an example in php.

function showForbidden()
header('HTTP/1.1 403 Forbidden');

<title>403 Forbidden</title>
<p>Directory access is forbidden.</p>
ดูคลิป November 02, 2008 at 04:01
thank for code

// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=""');
header('Content-Transfer-Encoding: binary');
// load the file to send:
Miroslav Asenov October 28, 2008 at 18:31
Mac xie - Some times header not work. See your error logs it is server error.
mac xie October 22, 2008 at 05:22
sometimes header not work ,but don't show any error message
code follow:
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location:{$url}" );
alfred March 01, 2008 at 03:42
To see headers I would really recommmend to use firebug which is the most awesome firefox extension for webdevelopers. This list is nice BTW.
ernest February 14, 2008 at 07:25
The headers for download section you have is really nice. If you want the window to give an estimated download time you can use Content-Length: filesize(file_name)