What are the best practices for handling user agent data in PHP?

When handling user agent data 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 built-in filter_var() function with the FILTER_SANITIZE_STRING filter to clean the user agent data before using it in your application.

$userAgent = filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_STRING);