PHP Headers and 301 Forwarding – Output Buffering ob_start() for Errors

PHP

PHP headers are used to send a message to the browser. The message can be something like forward to a new URL, give a 404 error, tell visitors that the page has moved permanently or give a 301 moved permanently status, tell the content type of the page, tell when the page expires or prompt the user to start downloading a file.

PHP headers should be the first thing that is outputted to the browser. So, remember to place headers() before any other php code that outputs something to the browser. You can perform calculations, but you cannot output anything before headers. If outputs are already sent, headers do not work and they give a headers already sent error. If you want, you can use Output Buffering to delay the output sent to the the browser so that the “Warning: Cannot modify header information – headers already sent by (output)” error can be avoided.

A simple use of header

<?php
header('Location: http://www.thisfox.com');
?>
Output:
Redirect to the website http://www.thisfox.com
Note: After redirection, any subsequent code placed after header is not executed.

301 Moved Permanently using Header

You can also redirect while sending a message that the page has been moved permanently, i.e. “301 Moved Permanently status”. This is useful for SEO.

Example:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.thisfox.com");
?>
Output:

301 Moved Permanently redirect to http://www.thisfox.com
If you did not use the 301 status, the header location code will give out a "302 Moved Temporarily" status.

Redirecting After Few Seconds

Headers can be used to redirect a user to another page after a few seconds. The time in between is used to give out a message that the user is being redirected.

Example:

<?php
header( "refresh:10;url=http://www.thisfox.com" );
echo 'You will be redirected in 10 seconds to www.thisfox.com.';
?>
Output:
Redirects to http://www.thisfox.com

404 Error using Header

If you need to throw a “404 Not Found” error, headers can do so. For that:

<?php
header("HTTP/1.0 404 Not Found");
echo "Error Not Found";
?>
Output: Gives a 404 Error header with the message
Error Not Found
Note: You can also give out other HTTP errors.

Facilitating a File Download using Header

You can use headers so that once it is activated, a file is downloaded. This is useful if you want to force download files like .jpg or .pdf files which normally open in a modern browser.

Example:

<?php
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="image.jpg"');
?>
Output:
Begins the download of image.jpg file.

Cache Control using Headers

Headers can also be used to tell the browser to not cache the contents of a certain page. You can set the expiry date as well. The expiry date is set in the past to disable caching.

Example:

<?php
header("Cache-control:no-cache, must-revalidate");
header("Expires: Tue, 22 Apr 2014 05:00:00 GMT");
?>

getallheaders()

We can use getallheaders() to get all the HTTP headers. This one stores the headers in an array.

Continuing to the above example:

<?php
header("Cache-control:no-cache, must-revalidate");
header("Expires: Tue, 22 Apr 2014 05:00:00 GMT");
foreach(getallheaders() as $key=>$value){
echo $key ." = ". $value;
echo "<br>";
}
?>
Output:
Accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding = gzip,deflate,sdch
Accept-Language = en-US,en;q=0.8,ar;q=0.6,hi;q=0.4
Cache-Control = max-age=0
Connection = keep-alive
Cookie = __utma=150413921.1099124555.1413612113.1413612113.1413612113.1; __utmz=160773921.1413612123.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host = yoursite.com
User-Agent = Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36

Headers and Output Buffering

Output buffering is used if there are code that are outputted before headers are sent. Without output buffering, you get an error.

Example:

<?php
echo "Hello";
header('Location: http://www.thisfox.com');
?>

With output buffering:

<?php
ob_start();
echo "Hello";
header('Location: http://www.thisfox.com');
ob_end();
?>