Instant Redirect in PHP
A redirect performed using the
header function does not happen at the moment
this function is called. This is because PHP itself
does not perform the redirect, but only sends
the corresponding HTTP header to the browser.
This means that the redirect will only occur when PHP finishes executing the entire script. Because of this, various parasitic effects can occur.
For example, in the following code we want to perform either a redirect or a database query. But the database query will be executed even if there was a redirect command:
<?php
if ($_GET['test']) {
header('Location: test.php');
}
$query = "UPDATE users SET changed=1 WHERE id=1";
mysqli_query($link, $query); // will execute even on redirect!
?>
To avoid such problems, you need to
call the
die function immediately after the redirect,
which will instantly terminate the
script execution and the redirect will happen
immediately:
<?php
if ($_GET['test']) {
header('Location: test.php');
die();
}
$query = "UPDATE users SET changed=1 WHERE id=1";
mysqli_query($link, $query);
?>
Reproduce some parasitic
effect in your code. Then fix it
using the die function.