What are some potential pitfalls of not properly handling variables passed through the URL in PHP?

If variables passed through the URL in PHP are not properly handled, it can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate these risks, it is important to sanitize and validate any user input received through the URL before using it in your application.

// Sanitize and validate the variable passed through the URL
$id = isset($_GET['id']) ? filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT) : null;

// Use the sanitized variable in your application
if ($id !== null) {
    // Perform actions with the sanitized $id
} else {
    // Handle the case where the variable is not valid
}