note 33456 added to function.eregi

From: Date: Thu, 26 Jun 2003 16:03:52 +0000
Subject: note 33456 added to function.eregi
Groups: php.notes 
Request: Send a blank email to php-notes+get-51047@lists.php.net to get a copy of this message
Keran's solution is a good one but I found it does not work for a small number of domains -- because of a bug in gethostbyname() that causes some valid domains not to resolve properly. The workaround is to test such domains with "www." added to the beginning. This script does all that and works perfectly for me. Enjoy! // Function to validate emails. function validate_email($email_raw) { // replace any ' ' and \n in the email $email_nr = eregi_replace("\n", "", $email_raw); $email = eregi_replace(" +", "", $email_nr); $email = strtolower( $email ); // do the eregi to look for bad characters if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*". "@([a-z0-9]+([\.-][a-z0-9]+))*$",$email) ){ // okay not a good email $feedback = 'Error: "' . $email . '" is not a valid e-mail address!'; return $feedback; } else { // okay now check the domain // split the email at the @ and check what's left $item = explode("@", $email); $domain = $item["1"]; if ( ( gethostbyname($domain) == $domain ) ) { if ( gethostbyname("www." . $domain) == "www." . $domain ) { $feedback = 'Error: "' . $domain . '" is most probably not a valid domain!'; return $feedback; } // ? $feedback = "valid"; return $feedback; } else { $feedback = "valid"; return $feedback; } } } ---- Manual Page -- http://www.php.net/manual/en/function.eregi.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33456 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33456&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33456&report=yes

« previous php.notes (#51047) next »