简体   繁体   English

FILTER_VALIDATE_EMAIL无效

[英]FILTER_VALIDATE_EMAIL not working

I certainly must be missing something here. 我当然一定要错过一些东西。 For some reason filter_var is not working. 由于某种原因,filter_var不起作用。 I'm trying to validate an email from $_POST, and it returns false even with valid emails. 我正在尝试验证来自$ _POST的电子邮件,即使有效的电子邮件也会返回false。 But, when I hardcode an email, it works fine. 但是,当我对电子邮件进行硬编码时,它运行正常。 What's wrong? 怎么了?

Here's my php code: 这是我的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();
}

Change the first email check: 更改第一封电子邮件检查:

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

It is getting the value " instead of checking for it. 它获得的价值"而不是检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM