这是用户邀请代码的片段。代码应该从以前的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的新手,但我发现自己每天都会回到这里寻找想法和社区的大力帮助!
答案 0 :(得分:3)
else { 'Whoops! It looks like you didn't actually add an email address...'; }
应该
else { echo 'Whoops! It looks like you didn't actually add an email address...'; }
答案 1 :(得分:0)
这是检查电子邮件地址的简短方法。
<?php
$re1='.*?'; //Non-greedy match on filler
$re2='([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})'; //Email Address 1
if ($c=preg_match_all ('/'.$re1.$re2.'/is', $text, $matches)) {
$email=$matches[1][0];
}
?>
$email
将包含$text
内的电子邮件或任何内容。您可以稍后查看empty()
。在我看来,域验证毫无意义。你可以说你想要的所有有趣的令人印象深刻的东西,我还没有看到一个现实世界的情况,这样的支票是必要的。