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

Method #2

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:

php
if ($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:

php
if ($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

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755