Building a PHP5 Form Processor: Coding the Form Validator Module - The big challenge: checking email addresses with the “validateEmail()” method (
Page 4 of 5 )
For anyone who has spent a long time writing validation routines, verifying email addresses is a challenging task. In the good old days, a robust email pattern coded in a regular expression was, most of the time, good enough to make sure that a user had entered a well-formed email address. Now, things are different, considering the vast number of current email domains. However, in order to keep the method’s source code simple and readable, I’ll walk through an intermediate example, and instruct the method to check only for well-formed addresses and existing domains in the DNS. Its signature is as follows:
public function validateEmail($field,$errorMessage){
if(!isset($_POST[$field])||!preg_match("/.+@.+\..+./",$_POST
[$field])||!checkdnsrr(array_pop(explode("@",$_POST
[$field])),"MX")){
$this->errors[]=$errorMessage;
}
}
As shown above, the method verifies whether the supplied email address is at least well-formed, without troubling things too much with complex patterns. It then searches in the DNS for the corresponding MX records, in order to determine whether the domain part corresponds to a real email domain. To perform this task, I use the “checkdnsrr()” PHP built-in function, which is extremely helpful when searching DNS records. Of course, this fact doesn’t mean that the user really exists, but it’s a decent way to check for the existence of a given email domain.
From this point onward, you can make your own way either by improving the effectiveness of this method or writing a full-fledged alternative solution, in order to verify user-supplied email addresses.
Now, in case you don’t know, the PHP “checkdnsrr()” function isn’t available on Windows systems, so here’s the alternative “windnsrr()”function, which essentially performs the same task on Microsoft servers:
private function windnsrr($hostName,$recType=''){
if(!empty($hostName)){
if($recType=='')$recType="MX";
exec("nslookup -type=$recType $hostName",$result);
foreach($result as $line){
if(preg_match("/^$hostName/",$line)){
return true;
}
}
return false;
}
return false;
}
And eventually, if you’re running your PHP applications on a Windows server, the “validateEmail()” method should be rewritten like this:
public function validateEmail($field,$errorMessage){
if(!isset($_POST[$field])||!preg_match("/.+@.+\..+./",$_POST
[$field])||!$this->windnsrr(array_pop(explode("@",$_POST
[$field])),"MX")){
$this->errors[]=$errorMessage;
}
}
After having defined the last checking method of the class, the only thing left to complete the definition of the form “validator” class consists of including two useful corollaries: the “checkErrors()” and “displayErrors()” methods.
| | Discuss Building a PHP5 Form Processor: Coding the Form Validator Module | | | | | | | The second article of this series walks through the development of the corresponding... | | | | | | Though in practical application you're not likely to run into this as often, a... | | | | | | First of all, I'd like to thank you for your comments on my article, and secondly... | | | | | | Hi Alejandro,
Thanks again for a great article. My compliments.
I have a... | | | | | | Hello Matthijs,
Thank you for your kind comments on this article. I simply used... | | | | | | hi,
thanks for this great article but will the third part be out..
eagerly... | | | | | | Isnt this... | | | | | | yep, that it. Thanks you for the link. | | | | | | Hi,
Thank you for your kind comments on this tutorial. Your feedback is really... | | | | | | Hey Rich, thanks for providing the correct link to the third article.
My Best... | | | | | | Hi,
great article(s),
but i have a question....the validateEmpty functions checks... | | | | | | Hi,
Thank you for commenting on my article. With regard to your question, if you... | | | | | | dont like the way this class relies on the global scope.. namely the _POST... | | | | | | Thank you for your suggestion. In fact, I mentioned this issue in the article too,... | | | | | | >>> Post your comment now! | | | | | |
|
 |