What security measures should be taken when handling parameters passed through a URL in PHP?

When handling parameters passed through a URL in PHP, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to do this is by using PHP's filter_input() function to filter and validate the input data before using it in your application.

// Sanitize and validate input parameters passed through URL
$user_id = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);
if($user_id === false) {
    // Handle invalid input error
    die('Invalid user ID');
}

// Use the sanitized and validated user_id in your application
echo "User ID: " . $user_id;