Re: Validating Email Conditional
| From: | Edward Kay | Date: | Wed, 01 Aug 2007 20:06:16 +0000 |
| Subject: | Re: Validating Email Conditional | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-260022@lists.php.net to get a copy of this message | ||
CK wrote:
Hi,
My script is working, but valid returns true even if the user is bogus. What needs changing so both conditions have to match, the following attempt returned "unexpected logical...."
if (getmxrr($domaintld,$mxrecords)) && if(fsockopen($domaintld,25,$errno,$errstr,30)) {
$valid = true;
}
You don't need to write the second 'if', just wrap the two conditionals in parentheses:
if ( getmxrr($domaintld,$mxrecords) && fsockopen($domaintld,25,$errno,$errstr,30) )
$valid = true;
You could equally use nested conditionals:
if (getmxrr($domaintld,$mxrecords)) {
if (fsockopen($domaintld,25,$errno,$errstr,30))
$valid = true;} There is no need for the second $valid = false in your code below, since you define that at the start. Edward
The following works, but needs the conditional mentioned... <?php $email = "lotus@bushidodeep.com"; $valid=""; function validate_email($email) { // Create the syntax of email with validation regular expression $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"; // Presume that the email is invalid $valid = false; // Validate the syntax if (eregi($regexp, $email)) {list($username,$domaintld) = split("@",$email); // Validate the domain if (getmxrr($domaintld,$mxrecords)) $valid = true; } // attempts a socket connection to mail serverif(fsockopen($domaintld,25,$errno,$errstr,30)) {$valid = true;} else {$valid = false;}return $valid;} if (validate_email($email)) echo "Email is valid!"; else echo "Email is invalid!"; ?> --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php