2012-07-22 67 views
3

我當然必須在這裏丟失一些東西。出於某種原因,filter_var不起作用。我試圖驗證來自$ _POST的電子郵件,即使使用有效的電子郵件,它也會返回false。但是,當我硬編碼一個電子郵件,它工作正常。怎麼了?FILTER_VALIDATE_EMAIL不工作

這裏是我的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 protected]+\./', $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

沒有'htmlspecialchars()'工作嗎? – 2012-07-22 06:18:44

+0

@Yan它不起作用 – irosenb 2012-07-22 06:23:19

回答

1

變化的第一個電子郵件檢查:

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

據所獲得的價值"而不是檢查它。

+0

OMG!我感到非常愚蠢。非常感謝。 ,,爲什麼沒有發生任何錯誤? – irosenb 2012-07-22 06:32:30

+1

@ undacovabroda45,你不應該像'if(empty($ email))那樣進行空檢查' – arma 2012-07-22 06:34:15

+0

@ undacovabroda45這不是錯誤。你正在'$ email'中插入一個值。 – 2012-07-22 06:37:27