PHP Headers
Author: brett | Posted On: 29 April 2007 | Views: 2161
PHP Headers
PHP Headers can perform a few things, some of them are listed below:
We will look at the above list in a few minutes but first i would like to explain why that "Headers already sent" error occurs.
Headers are preferred to be the first one to be sent to the browser. That is only when it can work properly. Sometimes coders make mistakes by adding either an empty line or echoing something before the header is set. Usually after session_start().
If you write your code like this
<?php
session_start();
?>
This might give the error. The reason for this is the empty line above the PHP tag. That line is rendered as HTML.
Similarly if you do something like this
<?php
echo "Welcome";
session_start();
?>
This will return the error. You cannot echo or put a blank line before the session_start() function.
The correct way is
<?php
session_start();
echo "Welcome";
?>
The core point here is not to output anything before the session_start() is executed.
Redirecting browser
You can redirect your user to some other page.You can do this by writing
<?php
header("Location: http://www.redirection_URL_here.com");
?>
The following command will redirect the browser window to the given location as soon as the command is executed.
Please note that "Location" starts with capital 'L', some browsers might not redirect if small 'l' is used.
Even though you redirected the user successfully, yet this is not the proper way to do it. The above command does not generate the 301 response which means the target page will loose a hit count. To avoid that, you the following code
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.redirection_URL_here.com");
?>
This is the proper way to do it so that the redirection URL receives a hit count.
Furthermore you can add some redirection interval by using the following code
<?php
$seconds = 5; //will redirect after 5 seconds
header("Refresh: $seconds; url=http://yourURLhere.com");
?>
Do not cache pages
You can prevent the browser to cache pages by using the following code
<?php
//Date in the past, tells the browser that the cache has expired
header("Expires: Mon, 20 Feb 2004 20:12:03 GMT");
/* The following tell the browser that the last modification is right not so it must load the page again */
header("Last-Modified: ". gmdate("D, d M Y H:i:s"). "GMT");
//HTTP/1.0
header("Pragma: no-cache");
?>
The above code will let the browser load the page fresh from the server every time it is called. The reason is simple, we tell the browser that the content just expired and it was just last modified too. Furthermore we also tell it Pragma: no-cache to make sure it gets a fresh server copy each time.
Content Types
You can generate different types of data other than HTML, for example, you can parse an image, psd, zip etc from PHP. If you do that, you need to tell the browser that the content i am sending is not HTML but something else.
<?php
//The browser will deal the page as PDF
header ( 'Content-type: application/pdf' );
//This will be called phpParsed.pdf
header ( 'Content-Disposition: attachment; filename="phpParsed.pdf"' );
?>
There are several other types which i will cover in some other tutorial.
Hope you enjoyed the tutorial and make use out of it.



