how to stop this code from executing the redirect
✔ Recommended Answer
You can use curl which does not follow redirects by default.
See Is there a way to follow redirects with command line cURL?
curl mysite.com
Source: stackoverflow.com
Answered By: Ruan Mendes
If you want to stop a redirect from happening in PHP, you can use the exit()
or die()
functions. These functions will immediately stop the execution of the script, so no further code will be executed.
Here's an example of how you could use exit()
or die()
to prevent a redirect:
phpif ($condition) {
// Redirect to another page
header('Location: https://www.example.com/newpage.php');
exit(); // Stop executing the script
}
// Code here will not be executed if the condition is true
In this example, if $condition
evaluates to true, the script will redirect to https://www.example.com/newpage.php
using the header()
function. The exit()
function is then used to stop executing the script, so any code after the redirect will not be executed.
Alternatively, you can also use an if...else
statement to conditionally execute the redirect:
phpif ($condition) {
// Redirect to another page
header('Location: https://www.example.com/newpage.php');
} else {
// Code to execute if condition is false
}
This approach uses an if...else
statement to conditionally execute the redirect. If $condition
is true, the redirect will be executed using header()
. If it is false, the code in the else
block will be executed instead.
Comments
Post a Comment