What are the best practices for handling URL parameters in PHP, especially when receiving data from external sources like SMS messages?

When handling URL parameters in PHP, especially when receiving data from external sources like SMS messages, 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 achieve this is by using PHP's filter_input() function with appropriate filters for the specific data being received.

// Example of handling URL parameters from external sources like SMS messages
$userId = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
if ($userId === false) {
    // Invalid user ID, handle error
} else {
    // Proceed with processing the user ID
}