php电子邮件格式,验证电子邮件格式PHP

这是用户邀请代码的片段.该代码应从先前的POST表单中获取电子邮件地址,然后检查其格式是否正确.我使用了一个名为checkMail的功能来完成这项工作.但是,似乎无论何时电子邮件格式错误或表单为空白时,它都不会显示以下行:

else if(checkEmail($_POST['email'])==false){ 'Whoops! Looks like the email address you selected is invalid :('; }

要么

else { 'Whoops! It looks like you didn't actually add an email address...'; }

已经被困了几个小时,试图调试…希望这里有人可以帮助我!

这是完整的代码:

if(!empty($_POST['email'])) {

if(checkEmail($_POST['email'])==true) {

$thisDomain = str_replace('www.', '', $_SERVER['HTTP_HOST']);

$mailcont = "Someone has invited you to an invite only website!\nYour invite code is: ".$_POST['code'].".\n\nYou can use it at http://".$thisDomain."?go=register&hash=".$_POST['code'];

if(sendEmail($_POST['email'],'You have been invited!',$mailcont,'noreply@'.$thisDomain)) {

echo 'Your invite was dispatched to '.$_POST['email'].'
Go back home';

} else { echo 'Whoops! Something went horribly wrong, and we couldn't send the email :('; }

} else if(checkEmail($_POST['email'])==false){ 'Whoops! Looks like the email address you selected is invalid :('; }

} else { 'Whoops! It looks like you didn't actually add an email address...'; }

break;

这是功能checkMail的代码:

function checkEmail($email)

/**

Validate an email address.

Provide email address (raw input)

Returns true if the email address has the email

address format and the domain exists.

*/

{

$isValid = true;

$atIndex = strrpos($email, "@");

if (is_bool($atIndex) && !$atIndex)

{

$isValid = false;

}

else

{

$domain = substr($email, $atIndex+1);

$local = substr($email, 0, $atIndex);

$localLen = strlen($local);

$domainLen = strlen($domain);

if ($localLen < 1 || $localLen > 64)

{

// local part length exceeded

$isValid = false;

}

else if ($domainLen < 1 || $domainLen > 255)

{

// domain part length exceeded

$isValid = false;

}

else if ($local[0] == '.' || $local[$localLen-1] == '.')

{

// local part starts or ends with '.'

$isValid = false;

}

else if (preg_match('/\\.\\./', $local))

{

// local part has two consecutive dots

$isValid = false;

}

else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))

{

// character not valid in domain part

$isValid = false;

}

else if (preg_match('/\\.\\./', $domain))

{

// domain part has two consecutive dots

$isValid = false;

}

else if

(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',

str_replace("\\\\","",$local)))

{

// character not valid in local part unless

// local part is quoted

if (!preg_match('/^"(\\\\"|[^"])+"$/',

str_replace("\\\\","",$local)))

{

$isValid = false;

}

}

if ($isValid && !(checkdnsrr($domain,"MX") ||

checkdnsrr($domain,"A")))

{

// domain not found in DNS

$isValid = false;

}

}

return $isValid;

}

在此先感谢大家.你们真棒,我是Stackoverflow的新手,但我发现自己每天都回到这里来寻求想法和社区的大力帮助!

解决方法:

否则{‘哇!看来您实际上并未添加电子邮件地址…’; }

应该

否则{echo’Whoops!看来您实际上并未添加电子邮件地址…’; }

标签:verification,email,mysql,php

来源: https://codeday.me/bug/20191201/2078447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值