{Web Space Tutorials}

good tutorials, made easy

PHP Headers


Some of you may be thinking, what is a php header? Basically a php header is a bit of php code that is sent to the server before anything else on your page and gives the server a set of instructions telling the server what to do when the rest of your page is sent. You may not know it but php headers are sent automatically every time your web page loads and are usually automated.

With php header codes we can edit this information and force the server to do things like re-directs, collect browser information, and more. This tutorial will teach you some of the basic, most used php headers.


For this tutorial it is recommended that you have:

How To Use php Headers

The first thing you need to know about php headers, are how to use them. All php headers must be declared at the very top of your web page, even before the doctype information. This is because if you do not declare any headers, the server will make them up for you. So if you're going to use a header, make sure to open your php script at the very top of the page, before anything else. Always remember that you need to begin php script with <?php and close php script with ?>.

Basic php Headers

Below are some of the basic php headers that you will use most often when coding for the web.

Header Location

The location header tells the server where the page is, we can use this header as a method of re-directing the user to a different page when the page loads. For example

header('Location: http://www.yahoo.com');

With that header when the page loads the browser will automatically re-direct the user to www.yahoo.com.

Header Refresh

The refresh header works a lot like the location header, except that instead of sending the user to a new page right away, it sends them after a set period of seconds.

header('Refresh: 10; url=http://www.yahoo.com/');

With that header when the page loads the browser re-direct the user to www.yahoo.com after 10 seconds.

Header Content Type

The content type header works by telling the server what kind of content it is about to get, thus telling the server how to read the content. We can manipulate this by telling the browser how to display a page. A common use of this is to make a web page print friendly.

header('Content-Type: text/plain');

With this content type the user will see your page just as if it were plain text. The only problem with this is that this will also display all of your html code as text as well. Keep this in mind.

Header Cache Control

With the cash control header we can control when the browser will update it's cache of a site. The cache is a computer's memory of your site. It will remember your web page if it has been there before, and information from it so that the next time the user visits your site, it will load faster.

What cache control does is sets the amount of time the browser holds on to this information before dumping it to get new information. We can tell the browser to get new information every time or hold on to the same information for a long time. This is useful for sites that constantly update such as a blog or shopping site or for pages that do not update too much like a resume site.

header('Cache-Control: no-cache,no-store,must-revalidate');
header('Expires: Fri,01 Jul 2011 00:00:00 GMT');
header('Pragma: no-cache');

With that code the browser will never have a cache of the site because the expiration time is set in the past, meaning it is always expired.

PHP Server globals

Server globals are a different kind of php header that aren't used in the same fashion as the others. These globals are referred to as request headers because instead of giving the server to information, they ask for information. Some server globals are very useful while others are situational. Keep in mind that unlike other headers, server globals can be called at any time.

Below are some of the super globals that you will come across.

    This code gets the name and version of the browser the user is using.
    The referer global returns what page the user used to get to the current page.;
    The host global returns what the hosting name of the page.
    The document root global returns the path of the server up to the document root.
  5. $_SERVER['PHP_SELF'];
    The self global returns the path from the site root up to the current page.
    The remote addr global returns the user's ip address.
    The server name global returns the name of the server the page is on.

Each of these server globals have their uses, but the most useful one's here are the document root and self globals. Remember that these globals will always pull document paths, meaning you can make links with them.

More Reading

This is a very brief tutorial of the most commonly used php headers. If you would like to read more on what php headers can do, you can read more about them at w3schools.

w3schools php headers


Now that you've read the tutorial, take the quiz.