最好的电子邮件验证功能在一般和特定(学院领域)?
嘿家伙,我知道电子邮件验证是其中的一件事,这不是最有趣的事情。 我创建了一个网站,我想限制我的观众仅限在我大学里的人,我也想为我的用户提供一个首选的电子邮件地址。 所以这是一个两部分问题。
电子邮件验证是否有真正可靠的PHP功能?
我可以验证来自特定域的电子邮件吗? 我不想只检查域是否存在,因为我知道www.mycollege.edu已经存在。 是否真的有办法验证用户是否拥有有效的@ mycollege.edu网址? 谢谢!
这是我使用的:
function check_email_address($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~.-]{0,63})|("[^(|")]{0,62}"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^[?[0-9.]+]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
编辑用preg_match代替depreciated ereg以符合PHP 5.3
如果你真的想确保它的有效性,使你的注册表单向他们发送一封带有URL链接的电子邮件,那么他们必须点击进行验证。
这样,您不仅知道该地址有效(因为收到了电子邮件),而且您还知道该帐户的所有者已注册(除非其他人知道他的登录详细信息)。
为了确保它正确结束,你可以在'@'上使用explode()并检查第二部分。
$arr = explode('@', $email_address);
if ($arr[1] == 'mycollege.edu')
{
// Then it's from your college
}
PHP还拥有使用filter_var验证电子邮件地址的方法:http://www.w3schools.com/php/filter_validate_email.asp
这应该工作:
if (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9._-])@mycollege.edu$/', $email)) {
// Valid
}
链接地址: http://www.djcxy.com/p/92727.html
上一篇: Best email validation function in general and specific (college domain)?
下一篇: validation for php