gpt4 book ai didi

php - 验证电子邮件格式 PHP

转载 作者:可可西里 更新时间:2023-11-01 08:20:54 26 4
gpt4 key购买 nike

这是一段用户邀请码。该代码应从之前的 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&#39;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&#39;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 的新手,但我发现自己每天都会回到这里寻求想法和社区的大力帮助!

最佳答案

else { '哎呀!看起来你实际上并没有添加电子邮件地址......'; 应该else { echo '哎呀!看起来你实际上并没有添加电子邮件地址......'; }

关于php - 验证电子邮件格式 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11266967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com