Are there best practices for restricting email submissions to specific domains in PHP web forms?
To restrict email submissions to specific domains in PHP web forms, you can validate the email address entered by the user and check if it belongs to the allowed domain. This can be done by extracting the domain from the email address and comparing it against a list of allowed domains. If the domain does not match any of the allowed domains, you can display an error message to the user.
<?php
// List of allowed domains
$allowed_domains = array("example.com", "test.com");
// Get the email address from the form submission
$email = $_POST['email'];
// Extract the domain from the email address
list($user, $domain) = explode('@', $email);
// Check if the domain is in the list of allowed domains
if (!in_array($domain, $allowed_domains)) {
echo "Email address must belong to one of the following domains: " . implode(", ", $allowed_domains);
// Additional code to handle the error, such as preventing form submission
} else {
// Email address is valid, continue with form processing
}
?>
Related Questions
- How can the issue of not getting the expected 9 rows of results be addressed in the fetchTickets() function?
- How can APIs be used to facilitate the process of updating PHP applications across different servers?
- What are some platform-independent methods to set and delete information at the start and end of a PHP script?