验证电子邮件格式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'].'<br /><br />Go back <a href="?go=home">home</a>';
} 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