在注册脚本中集成FILTER_VALIDATE_EMAIL

时间:2013-09-02 17:54:44

标签: php mysql email

我希望在用户想要注册时集成FILTER_VALIDATE_EMAIL过滤器。

我有这个输入:

<input type="text" name="email" maxlength="255" />

代码的某些部分:

  • $ email = strip_tags(mysql_real_escape_string($ _ POST ['email']));
  • $ email2 = mysql_query(“SELECT * FROM users WHERE email ='{$ email}'”);

和此:

if(mysql_num_rows($email2) == 1)
{
echo "This email address is already in used.";
}

如果您需要更多代码,请告诉我们。谢谢!
这是我第一次使用MySQL,做得好! 或其他验证电子邮件的方式,ofc。

3 个答案:

答案 0 :(得分:0)

如何使用的示例:

$email = "someone@example.com";

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
  echo "E-mail is not valid";//Handle not valid email
}else{
  $result = mysql_query("SELECT * FROM users WHERE email = '{$email}'");
  //handle $result of DB
}

From Manual

另请注意:自PHP 5.5.0起,不推荐使用Mysql_ *扩展,将来也会删除。相反,应该使用MySQLi或PDO_MySQL扩展。

有用的链接Why shouldn't I use mysql_* functions in PHP

答案 1 :(得分:0)

请尝试以下代码:

$email = strip_tags(mysql_real_escape_string($_POST['email']));

if(filter_var($email, FILTER_VALIDATE_EMAIL)) { // Email is valid
    $result = mysql_query("SELECT * FROM users WHERE email = '{$email}'");

   if(mysql_num_rows($result)) {
      echo "This email address is already in used.";
    }
} else {
    echo "E-mail is not valid";
}

答案 2 :(得分:0)

$email_a = 'mail_check@hotmail.com';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_a) email address is considered valid."; 
   //reported as valid
} else {<br />
    echo "This email is invalid";
}

这是filter_var函数的一个示例,其中包含指定的过滤器(FILTER_VALIDATE_EMAIL)用法..