What are the potential security risks associated with using the header(location: ...) function in PHP?

The potential security risk associated with using the header(location: ...) function in PHP is that it can be vulnerable to header injection attacks. To mitigate this risk, it is recommended to sanitize and validate the input before using it in the header function to prevent malicious redirections.

// Sanitize and validate the input before using it in the header function
$redirect_url = filter_var($_GET['redirect_url'], FILTER_VALIDATE_URL);

if ($redirect_url !== false) {
    header("Location: " . $redirect_url);
    exit();
} else {
    // Handle invalid input
    echo "Invalid redirect URL";
}