Modify HTTP Headers (Examples)

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

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

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
04/03/2006

Updated:
09/14/2007

Tags:
, ,

Related links:
- List of HTTP status codes
- List of HTTP status codes
- More detailed tutorial
- Caching Tutorial
- Detailed tutorial (German)


// 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:
header('Location: http://www.example.org/');
 
// Redriect with a delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';
 
// you can also use the HTML syntax:
// <meta http-equiv="refresh" content="10;http://www.example.org/ />
 
// 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="example.zip"'); 
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');
 
// 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';


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

<script>document.body.style.display=none;</script>
Simranjit singh April 22, 2010 at 09:02
Hello i really appriciate you. Thanks for providing such a great informtion!!
Danko - LogoFoo.com 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
คอนโด April 07, 2010 at 16:28
header('Content-Type: application/x-shockwave-flash'); // Flash animation

not work on firefox ???
but ok on ie
zzzz February 08, 2010 at 09:23
this is a test
Jignesh February 23, 2009 at 11:56
erere
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');

echo("<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>");
}
ดูคลิป November 02, 2008 at 04:01
thank for code

// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');
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}" );
exit();
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)