3

我当然必须在这里遗漏一些东西。由于某种原因 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();
}
4

1 回答 1

1

更改第一个电子邮件检查:

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

它正在获取价值"而不是检查它。

于 2012-07-22T06:25:32.003 回答