How can PHP developers prevent excessive redirection loops in their code?

To prevent excessive redirection loops in PHP code, developers can track the number of times a page has been redirected and set a limit to prevent infinite redirection. By checking the number of redirects and redirecting only up to a certain limit, developers can avoid getting stuck in a loop.

$redirect_count = isset($_SESSION['redirect_count']) ? $_SESSION['redirect_count'] : 0;

if($redirect_count < 3) {
    $_SESSION['redirect_count'] = $redirect_count + 1;
    header('Location: new_page.php');
} else {
    // Redirect limit reached, handle appropriately
}