FILTER_VALIDATE_EMAIL无效

时间:2012-07-22 06:12:56

标签: php validation email-validation filter-var

我当然必须在这里遗漏一些东西。由于某种原因,filter_var不起作用。我正在尝试验证来自$ _POST的电子邮件,即使有效的电子邮件也会返回false。但是,当我对电子邮件进行硬编码时,它运行正常。怎么了?

这是我的PHP代码:

function redirect() { //redirecting to home page function. Used in one of the lectures.
    $host = $_SERVER["HTTP_HOST"]; 
    $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
    header("Location: http://$host$path/index.php");
    exit;
}

try 
{
    $dbh = new PDO($db, $dbuser, $dbpassword);
}
catch (PDOException $e) 
{
    echo "Connection failure: " . $e->getmessage();
}

if (!isset($_POST['email']) || !isset($_POST['password1']) || !isset($_POST['password2'])) {
    redirect();
}

$password1 = htmlspecialchars($_POST['password1']);
$email = htmlspecialchars($_POST['email']);
$password2 = htmlspecialchars($_POST['password2']);
//preg_match('/.+@.+\./', $email) == FALSE



if ($email = "") {
    print "email not there";

} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    print "not real email";

} elseif (strlen($password1) < 6) {
    print("password too small");

}  elseif (!(preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $password1))) {
    print "numbers and letters plz";

} elseif ($password1 != $password2) {
    print "passwords not same";
    //redirect();
}

1 个答案:

答案 0 :(得分:1)

更改第一封电子邮件:

if ($email == "") {
    print "email not there";
}

获取值"而不是检查它。