What are some potential pitfalls of using a whitelist approach for email validation in PHP?
One potential pitfall of using a whitelist approach for email validation in PHP is that it can be too restrictive and may block legitimate email addresses that are not on the whitelist. To address this issue, you can combine the whitelist approach with a secondary validation method, such as using regular expressions to ensure that the email address format is correct.
$email = "example@example.com";
// Whitelist of allowed email domains
$allowed_domains = array("example.com", "test.com");
// Check if email domain is in the whitelist
$email_parts = explode("@", $email);
if (in_array($email_parts[1], $allowed_domains) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email is valid";
} else {
echo "Email is not valid";
}