What is HTTP Headers and how to set php HTTP Headers?

The header() function sends a raw HTTP header to a client or browser or simply we can say it tells browser what type of content,it have to show. When a request sent from server to client/browser the HTTP informations has been sent to browser,The HTTP information may be small or large depends on the page and content shown.

 

A small HTTP information sent by browser are

HTTP/1.1 301 Moved Permanently =>

Date => Fri, 25 Aug 2011 02:00:03 GMT
Server => Apache
X-Powered-By => PHP/5.3.0
X-Pingback => http://www.scriptarticle.com/xmlrpc.php
Location => http://www.scriptarticle.com/
Content-Length => 0
Connection => close
Content-Type => text/html; charset=UTF-8

 

It is important to notice that header()  always be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):
for output buffering simply, a PHP function is used

ob_start();

PHP is not limited to output only html. PHP can output images, pdf, JavaScript files as well. Browsers determine what type of content is by analysing the headers sent.To send PHP header use the function header(). You have to call this function before output shown. Use the function headers_sent() to check whether the headers have been sent and output started.

There are various headers used, here are some examples.

 

// Status code (301,302,404,403) headers
// Use this header instruction to fix 404 headers
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 redirections, because search engines always know
// what’s going on and it can easily update their urls in the web master tools
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.scriptarticle.com/’);

 

// Redirect with a delay:
header(‘Refresh: 10; url=http://www.scriptarticle.com/’);
print ‘You will be redirected in 10 seconds’;

 

// you can also use the HTML syntax
// <meta http-equiv=”refresh” content=”10;http://www.scriptarticle.com/” />

// 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: 5000’);

 

// Disable caching of the current document
header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate’);
header(‘Expires: Mon, 26 Jul 1999 05:00:00 GMT’); // Date of  past
header(‘Pragma: no-cache’);

 

// set content type (page have content of 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: application/x-shockwave-flash’); // Flash animation
header(‘Content-Type: image/jpeg’); // JPG picture
header(‘Content-Type: application/pdf’); // PDF file
header(‘Content-Type: audio/mpeg’); // Audio MPEG (MP3,…) file
header(‘Content-Type: application/zip’); // ZIP file

 

// 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’;

 

// Headers for an download
header(“Cache-Control: public”);
header(“Content-Description: File Transfer”);
header(“Content-Disposition: attachment; filename=$file”);
header(“Content-Type: application/zip”);
header(“Content-Transfer-Encoding: binary”);