Headers must be set before any data is transmitted, so you can't just stick them in the middle of a file. Quoting the the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

So at the very least you'll need to rewrite your file to:

<?php

  header("Window-target: _parent");
  header("Location: https://www.w3schools.com/");

?>
<!doctype html>
...  

Also, never sleep() in an http(s) response: that response should finish as fast as it can, no matter what content it needs to generate. Sleep has no place in (really any) PHP code.

Answer from Mike 'Pomax' Kamermans on Stack Overflow
๐ŸŒ
W3Schools
w3schools.in โ€บ php โ€บ php-headers
PHP Header Function - W3Schools
<?php header("Refresh: 5; url=http://www.example.com"); //will redirect after 5 seconds ?> You can prevent the browser to cache pages by using the following code: <?php //Date in the past, tells the browser that the cache has expired header("Expires: Mon, 20 Feb 2005 20:12:03 GMT"); /* The following tell the browser that the last modification is right not so it must load the page again */ header("Last-Modified: ".
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to Redirect a Web Page with PHP
In PHP, you can use several functions to make a redirect from one web page to another. Learn how to do it accurately with W3docs.
Discussions

php redirect on successful login - Stack Overflow
I have a .php that registers a user by calling another php function but i dont know what the best way to redirect a user if the registration is successful. Is there an option other than header inca... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I make a redirect in PHP? - Stack Overflow
I chose to include it in my answer ... a simple redirect, one might as well play safe rather than not implement a simple yet crucial step just so he is able to take advantage of advanced process control. 2013-06-05T17:24:12.473Z+00:00 ... But browsers that respect the header will leave the page and close the connection while your script is still executing. This is totally bad. PHP will go on ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
W3schools finally fixed the insecure code in their PHP/MySQL tutorial!
Even though I still think it's a bad site overall, at least they are (sometimes) receptive to suggestions. That vulnerability has been there for almost eight years . I reported it to them more than once and so did other people. I think "sometimes receptive" is vastly overstating how much they give a shit about fucking over newbies. And like bravadomizzou said, you can't just wrap it in mysql_real_escape_string and be secure. That code is still a security hole waiting to happen. More on reddit.com
๐ŸŒ r/webdev
37
86
April 12, 2014
Can i block W3Schools from my google search results?
I mean w3schools tutorials might be bad but if you search for something like "Javascript date" the w3schools will most probably have the right answer for the right syntax. It's good for CSS too. More on reddit.com
๐ŸŒ r/learnprogramming
17
15
May 18, 2016
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ php-basic-exercise-11.php
PHP Exercise: Redirect a user to a different page - w3resource
3 weeks ago - It's a best practice to use exit(); or die(); right after the header() function to stop further code execution, ensuring the redirection happens immediately. ... replace: The optional replace parameter indicates whether the header should replace ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-make-a-redirect-in-php
How to make a redirect in PHP? - GeeksforGeeks
July 12, 2025 - Below program illustrates the header() function in PHP: Program: ... Note: The die() or exit() function after header is mandatory. If die() or exit() is not put after the header('Location: ....') then script may continue resulting in unexpected behavior. For example, result in content being disclosed that actually wanted to prevent with the redirect (HTTP 301).
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_network_headers_sent.asp
PHP headers_sent() Function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
VSYS
vsys.host โ€บ how-to โ€บ how-to-make-a-redirect-in-php
How to Make a Redirect in PHP โ˜… VSYS Tutorials
August 8, 2024 - This guide provides an overview of two methods for implementing PHP redirects: using the PHP Header function and JavaScript through PHP โ˜… VSYS Tutorials
Find elsewhere
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-make-a-redirect-in-php.php
How to Make a Redirect in PHP
If you want to redirect the users from old page to a new page on a permanent basis then also mention HTTP response code in the header() function as shown in the following example, so that search engines transfer "page rank" from the old page ...
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php tutorial โ€บ php redirection
PHP Redirection - PHP Tutorial
April 6, 2025 - The following diagram illustrates how redirection works: How it works. First, the web browser requests the URL /about.php from the server. Second, the web server receives the request to the /about.php page and informs the web browser of the new URL: /about-us.php.
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_js_redirect_webpage.asp
How To Redirect to Another Webpage
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
Elementor
elementor.com โ€บ blog โ€บ resources โ€บ php redirects: how to make a redirect in php? {8 methods}
PHP Redirects: How to Make a Redirect in PHP? {8 Methods}
November 30, 2025 - Importantly, unlike a 302 redirect, a 307 redirect instructs the browser or user agent that it must reuse the same HTTP method (GET, POST, etc.) it originally used when accessing the new location. Hereโ€™s the core PHP code to perform a 307 Temporary Redirect:
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ web servers โ€บ how to make a redirect in php
How to Make a Redirect in PHP (2 Methods) | phoenixNAP KB
December 16, 2025 - Use this comprehensive guide to set up a PHP redirect using the PHP header function or by calling a JavaScript function via PHP.
๐ŸŒ
W3Schools
w3schools.invisionzone.com โ€บ server scripting โ€บ php
How To Redirect To Another Page In 5 Seconds - PHP - W3Schools Forum
March 21, 2009 - i used header("Location:http://www.google.com"); but it was not successful and gave a error. Can u please help me
๐ŸŒ
W3Schools
w3schools.com โ€บ tags โ€บ tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
Top answer
1 of 16
2114

Summary of existing answers plus my own two cents:

1. Basic answer

You can use the header() function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <!DOCTYPE ...> declaration, for example).

header('Location: '.$newURL);

2. Important details

die() or exit()

header("Location: https://example.com/myOtherPage.php");
die();

Why you should use die() or exit(): The Daily WTF

Absolute or relative URL

Since June 2014 both absolute and relative URLs can be used. See RFC 7231 which had replaced the old RFC 2616, where only absolute URLs were allowed.

Status Codes

PHP's "Location"-header still uses the HTTP 302-redirect code, this is a "temporary" redirect and may not be the one you should use. You should consider either 301 (permanent redirect) or 303 (other).

Note: W3C mentions that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.

3. Documentation

HTTP Headers and the header() function in PHP

  • What the PHP manual says
  • What Wikipedia says
  • What the W3C says

4. Alternatives

You may use the alternative method of http_redirect($url); which needs the PECL package pecl to be installed.

5. Helper Functions

This function doesn't incorporate the 303 status code:

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

    exit();
}

Redirect('https://example.com/', false);

This is more flexible:

function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}

6. Workaround

As mentioned header() redirects only work before anything is written out. They usually fail if invoked inmidst HTML output. Then you might use a HTML header workaround (not very professional!) like:

 <meta http-equiv="refresh" content="0;url=finalpage.html">

Or a JavaScript redirect even.

window.location.replace("https://example.com/");
2 of 16
165

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:

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

Result of three executions:

bart@hal9k:~> cat /tmp/track.txt
127.0.0.1 2009-04-21T09:50:02+02:00
127.0.0.1 2009-04-21T09:50:05+02:00
127.0.0.1 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.

๐ŸŒ
Hostinger
hostinger.com โ€บ home โ€บ tutorials โ€บ how to make a php redirect
How to make a PHP redirect
November 19, 2025 - Learn how to make a PHP redirect with the header() function. We cover 301 and 302 status codes, conditional redirects, and how to fix PHP errors.
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ php โ€บ php scripts
How to Redirect With PHP | Envato Tuts+ - Code
May 21, 2020 - The server receives the index.php file request and wants to inform the client that itโ€™s no longer available or moved somewhere else, and it should look to a new file instead: new_index.php. The server sends the Location header with a new URL along with the 301 or 302 HTTP code. These are the HTTP codes for redirection.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-redirect
PHP Redirect - PHPpot
July 2, 2022 - PHP redirect mechanism is used to navigate the user from one page to another without clicking any hyperlinks. This will be helpful in such c