How do I make a redirect in PHP?


Is it possible to redirect a user to a different page through the use of PHP?

Say the user goes to and I want to redirect them to, how would I do so without the use of a meta refresh? Is it possible?

This could even protect my pages from unauthorized users.

10/17/2019 10:22:45 AM

Use the header() function to send an HTTP Location header:

header('Location: '.$newURL);

Contrary to what some think, die() has nothing to do with redirection. Use it only if you want to redirect instead of normal execution.

File example.php:

    header('Location: static.html');
    $fh = fopen('/tmp/track.txt', 'a');
    fwrite($fh, $_SERVER['REMOTE_ADDR'] . ' ' . date('c') . "\n");

Result of three executions:

[email protected]:~> cat /tmp/track.txt 2009-04-21T09:50:02+02:00 2009-04-21T09:50:05+02:00 2009-04-21T09:50:08+02:00

Resuming — obligatory die()/exit() is some urban legend that has nothing to do with actual PHP. It has nothing to do with client "respecting" the Location: header. Sending a header does not stop PHP execution, regardless of the client used.


function Redirect($url, $permanent = false)
    if (headers_sent() === false)
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);


Redirect('', false);

Don't forget to die()/exit()!


Output JavaScript from PHP using echo, which will do the job.

echo '<script type="text/javascript">
           window.location = ""

You can't really do it in PHP unless you buffer the page output and then later check for redirect condition. That might be too much of a hassle. Remember that headers are the first thing that is sent from the page. Most of the redirect is usually required later in the page. For that you have to buffer all the output of the page and check for redirect condition later. At that point you can either redirect page user header() or simply echo the buffered output.

For more about buffering (advantages)

What is output buffering?


1. Using header function with exit()

     header('Location: target-page.php');

but if you use header function then some times you will get "warning like header already send" to resolve that do not echo or print before sending headers or you can simply use die() or exit() after header function.

2. Without header

    echo "<script>location.href='target-page.php';</script>";

here you will not face any problem

3. Using header function with ob_start() and ob_end_flush()

ob_start(); //this should be first line of your page
header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page

Most of these answers are forgetting a very important step!

header("Location: myOtherPage.php");

Leaving that vital second line out might see you end up on The Daily WTF. The problem is that browsers do not have to respect the headers which your page return, so with headers being ignored, the rest of the page will be executed without a redirect.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]