How can the use of header("Location: URL") in PHP scripts lead to an infinite loop?

Using header("Location: URL") in PHP scripts can lead to an infinite loop if the URL specified in the header function redirects back to the same script. To prevent this, you can add a condition to check if the current URL is the same as the redirect URL before executing the header function.

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$redirect_url = "http://example.com/redirected_page.php";

if($current_url != $redirect_url){
    header("Location: $redirect_url");
    exit();
}