Php function for validating email addresses
If the application is going to make use of a basic level of validation, a quick-and-dirty way to handle the situation might be to implement a simple PHP function that performs pattern matching to a standardized email address format, as we have seen many times.
However, when a deeper and more complex validation is required, we should take a look at well-trusted validation classes, such as Pear’s HTML_Quick Form class, or many other validation classes widely available out there.
Whatever the case, validating a visitor’s email address to see if it belongs to a real domain is always a good step to help you avoid, at least partially, several possible problems that arise when applications are receiving incoming bogus data.
Therefore, it‘s pretty easy to code a new function, which will take care of checking the existence of the corresponding MX entries for a given host.
Let’s write the function to do that: The above function accepts a string as an email parameter for checking whether it fits the proper format, and whether the domain is real.
To do this, we can use a couple of PHP lookup functions that come in handy for addressing these problems.
The checkdnsrr() function checks DNS records corresponding to a given Internet host name or IP address.
It searches the DNS for records of a specific type corresponding to the given host, returning true if any records are found, or returning false if no records are found or if an error occurs.