How can PHP developers prevent duplicate key-value pairs in the URL for security purposes?

Duplicate key-value pairs in the URL can potentially lead to security vulnerabilities such as injection attacks or data manipulation. To prevent this, PHP developers can sanitize and validate the input data before processing it. One way to avoid duplicate key-value pairs is to check for existing keys in the URL parameters and handle them accordingly.

// Prevent duplicate key-value pairs in the URL
$pairs = array();
foreach ($_GET as $key => $value) {
    if (!isset($pairs[$key])) {
        $pairs[$key] = $value;
    }
}

// Process the sanitized URL parameters
foreach ($pairs as $key => $value) {
    // Your processing logic here
}