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.
php redirect on successful login - Stack Overflow
How do I make a redirect in PHP? - Stack Overflow
W3schools finally fixed the insecure code in their PHP/MySQL tutorial!
Can i block W3Schools from my google search results?
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.
A combination of PHP and JS seems to be the easiest solution. But that might be only my opinion. I tried to document the code as good as possible, so others can understand it:
<?php
function redirect() { // Create some JS code which will pause for 3 seconds and execute the move function afterwards. This Function will redirect the user
echo "<script>";
echo "function move() {window.location.replace('http://www.w3schools.com');}";
echo "setTimeout(move, 3000);";
echo "</script>";
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Test2</h1>
<?php
echo "<p>You will be redirected after the code has been executed!</p>";
// Run actual code
redirect(); // Redirect using JS code
?>
</body>
</html>
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/");
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.