How to Redirect to Another Page in PHP

April 18, 2026 by Talal khalid

You can redirect users to another page in PHP using the header() function. This sends a redirect response to the browser.

PHP Code

header("Location: https://example.com");
exit;

This will redirect users to example.com immediately.

Redirect After a Delay

PHP itself cannot delay redirects like HTML meta refresh. However, you can use sleep() before redirecting:

sleep(5);
header("Location: https://example.com");
exit;

This redirects users after 5 seconds.

Helper Function

You can create a reusable function for redirects:

function redirect($url, $delay = 0) {
    if ($delay > 0) {
        sleep($delay);
    }
    
    header("Location: " . $url);
    exit;
}

Usage

redirect("https://example.com"); // Instant redirect
redirect("https://example.com", 5); // Redirect after 5 seconds

Note

Make sure no output is sent before calling header(). Otherwise, you may get a “headers already sent” error.

For better SEO and user experience, server-side redirects like this are recommended over HTML meta refresh.

Shares