What are some potential pitfalls of using global variables like $_ENV or $_SERVER in PHP scripts?

Using global variables like $_ENV or $_SERVER in PHP scripts can lead to security vulnerabilities such as injection attacks or unintended data exposure. To mitigate these risks, it is recommended to sanitize and validate any input from these variables before using them in your code.

// Example of sanitizing and validating input from $_ENV
$email = filter_input(INPUT_ENV, 'USER_EMAIL', FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Proceed with using the sanitized and validated email address
} else {
    // Handle invalid email address input
}