What are best practices for handling URL parameters in PHP?

When handling URL parameters in PHP, it is important to properly sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One best practice is to use PHP's filter_input function to retrieve and sanitize URL parameters before using them in your code.

// Retrieve and sanitize URL parameters using filter_input
$param1 = filter_input(INPUT_GET, 'param1', FILTER_SANITIZE_STRING);
$param2 = filter_input(INPUT_GET, 'param2', FILTER_SANITIZE_NUMBER_INT);

// Validate the parameters before using them in your code
if ($param1 !== false && $param2 !== false) {
    // Use the sanitized and validated parameters in your code
    echo "Parameter 1: " . $param1 . "<br>";
    echo "Parameter 2: " . $param2;
} else {
    // Handle invalid parameters
    echo "Invalid parameters";
}