Web developers have multiple options for redirecting/reloading pages using either a server-side scripting language, like PHP, or client-side JavaScript. In this article, I will discuss the options you have for ‘refreshing’ a page using PHP, and why that word is a slight bit misleading.

Solid Tip: PHP is limited to using a meta tag to actually ‘refresh’ a page. ‘Redirecting’ a page is another story.

Why refresh is the wrong word

The term ‘refresh’ suggests that a page existed already. This makes sense on the client-side when using JavaScript, as a page that a visitor is looking at can be ‘refreshed’ at any moment with a simple function call. Refresh is an incorrect term when talking about PHP. Since PHP is a server-side language, it can only take you to a different URL before any part of the page has been sent to your browser.

Your options

It is usually more appropriate to use the term ‘redirect’ when talking about PHP. You can redirect a page, before any information has been sent out, by using PHP’s built-in header() function. This is useful when processing forms (logging in a user) or checking that certain conditions are met (sending unauthorized users back to login page). The following example shows you how to redirect a page.

    header("location: http://solidlystated.com");

The header function is also used when you want to take advantage of the limited refresh capability of meta tags. You have probably seen this when a page tells you, “You will be redirected in X seconds, or you can click here.” You can also simply echo the HTML for the meta tag to accomplish the same thing.

    // this refreshes current page after 5 seconds.
    header( "refresh:5;" );
    // OR send to a new URL like this
    header( "refresh:5; url=http://solidlystated.com" );
    // OR simply echo the HTML meta tag
    echo '<meta http-equiv="refresh" content="5" />';