What are some best practices for securely handling user input in PHP header redirection to prevent potential security risks?

When handling user input in PHP header redirection, it is essential to sanitize and validate the input to prevent potential security risks such as header injection attacks. One best practice is to use functions like filter_var() or htmlspecialchars() to sanitize the input before using it in the header() function to redirect the user.

$user_input = $_POST['input'];

// Sanitize the user input using htmlspecialchars()
$sanitized_input = htmlspecialchars($user_input);

// Validate the input further if needed

// Perform the header redirection with the sanitized input
header("Location: /some_page.php?input=" . $sanitized_input);
exit();