PHP’s Location and Refresh

Programming with PHP to create dynamic pages is a necessary skill for any web developer and I’ve long believed the day of the HTML static page is numbered in the very small zeros. People want dynamic websites that actually allow them to fulfil functions and not the plain ad-hoc pages put up by rote as budget solutions. The designer should be identifying stuff users will want to do on the site, assessing how transactions and interactions can be streamlined and made more usable and interesting for them. Of course there’s other technologies like ASP for developing dynamic content too but for this article I’ll stick to mentioning PHP.

One of the most important things to understand is the HTTP protocol even if its only on a basic level. Its a stateless protocol meaning it has no memory once it leaves the page, and HTTP works by sending requests and responses to and from the server for content. These HTTP transactions therefore contain request and response headers. PHP scripts can send HTTP response headers using the header() function.

It should be noted at this point that header must be called before any other output is sent to the browser or you’ll get a world full of kick arse error in your face. That means not even a DTD declaration or an invisible character or any output whatsoever and its one of the major forum questions for new PHP developers wanting to know why their page isn’t working.

There are a number of headers you can send but the one’s I’m concerned with today are quite useful to note. The first is the location header which allows you to redirect the page in your programming conditionals, such as if the user does this action send them to that page but if they do another then send them to the alternate page. Or you can simply use the location header to redirect users as a control mechanism. The format for this is in the following example:

header("location: pagetogoto.php");

Similarly the refresh header forces the browser to reload a new page after a set interval of time. The format for this is in the following example:

header("refresh: 5; url=pagetogoto.php");

Its really pretty simple stuff but also quite useful, just remember its kind of pointless putting HTML on a page with the location header but kind of useful with the refresh header. I tend to use the refresh header to create a graceful thankyou message after contact form submissions, for example. And the location header is handy to peel off different pages to successful submissions and failed submissions, each of which redirect the user to separate destinations - success to the home page and failure back to the contact form.

2 Responses to “PHP’s Location and Refresh”

  1. Rambling Thoughts » PHP header(), location & refresh - Says:

    […] nse headers using the header() function… Read more of Steven’s article about PHP’s Location and Refresh Posted by: Neerav @ 12:50 pm […]

  2. Rambling Thoughts Says:

    PHP header(), location & refresh
    Steven Clark has written a useful little article about using PHP’s header() function to redirect users after form submissions etc which I have just implemented on my Contact form

    Excerpt: One of the most important things to understand is the HTTP p…