Refresh / Redirect with PHP

We know how to use theĀ Refresh meta tag in our <head> tags. But what if we needed to do the same thing, only this time, do it in PHP?

We can use PHP’s header() function

We can redirect or refresh our web pages simply by doing either of the following in our PHP scripts or codes:

Method I: using header(‘location…’)

You probably know this one already:

// refresh / re-direct without delay
// ---------------------------------
header( 'location:http://www.desilva.biz/webdsn/' );

Method II: using header(‘refresh…’)

// refresh / redirect to an internal web page
// ------------------------------------------
header( 'refresh: 5; url=/webdsn/' );
echo '<h1>You will be re-directed in 5 seconds...</h1>';

// refresh / redirect to an internal web page
// ------------------------------------------
header( 'refresh: 3; url=/' ); # redirects to our homepage
echo '<h1>You will be re-directed in 3 seconds...</h1>';

// refresh / redirect to an external web page
// ------------------------------------------
header( 'refresh: 0; url=http://www.example.net' );
echo '<h1>You won\'t know what hit you!</h1>';

You can of course use the header() function for other useful stuff as well. I definitely recommend reading up on the function at PHP.NET and this other place.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>