What are some common security vulnerabilities in PHP when using custom headers in REST APIs?

One common security vulnerability when using custom headers in REST APIs in PHP is the lack of proper validation and sanitization of user input. This can lead to potential attacks such as injection or manipulation of headers. To prevent this, it is important to always validate and sanitize input before using it in custom headers.

// Validate and sanitize input before using it in custom headers
$user_input = $_POST['user_input'];

// Validate input
if (!filter_var($user_input, FILTER_VALIDATE_INT)) {
    die('Invalid input');
}

// Sanitize input
$sanitized_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Set custom header with sanitized input
header('X-User-Input: ' . $sanitized_input);