What are some potential security risks associated with using header() in PHP?

Using header() in PHP can potentially lead to security risks such as header injection attacks. To mitigate this risk, it is important to validate and sanitize any user input before using it in header() functions. This can help prevent malicious users from injecting harmful code into the headers of the HTTP response.

// Validate and sanitize user input before using it in header()
$user_input = $_POST['user_input'];
$sanitized_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Use the sanitized input in header() function
header("Location: /somepage.php?input=" . $sanitized_input);