2013-04-13 247 views
-1

我剛纔問了一個類似的問題,但我想我現在更接近答案。函數返回值

我有一個函數在用戶的電子郵件地址和密碼登錄到配置文件之前驗證用戶的電子郵件地址和密碼。

它生成一個錯誤消息,如果電子郵件不正確;它迴應出「找不到匹配的電子郵件」。

當我輸入正確的電子郵件和密碼時,沒有任何東西會被返回。但如果我輸入錯誤的密碼和錯誤的電子郵件,它會給我「沒有找到匹配的電子郵件」。同樣的事情發生在正確的電子郵件,但密碼錯誤。

我試圖用fetchAllfetchColum返回,但是當我輸入正確的值時我仍然得到相同的循環。

任何想法?

function valid_credientials($email,$password){ 
    global $db; 

    $q = "SELECT * FROM user WHERE email = :email AND password =:password"; 
    $query = $db->prepare($q); 
    $query->execute(array(":email" => $email, ":password" => $password)); 
    $results = $query->fetchAll(); 

    if($results !=FALSE && $query->rowCount() > 0) { 
     if($results[0]['password'] == $password){ 
      $_SESSION['email'] = $email; 
      return $query->fetchAll($q,0)==1 ? true:false; 
     } 
    } 

    // return false by default 
    return false; 
} 

的後isset

if (isset($_POST['email'], $_POST['password'])) { 
    if (valid_credientials($_POST['email'], $_POST['password']) == false) { 
     $errors[] = 'No matching email found.'; 
    } 

    if (empty($errors)) { 
     $_SESSION['email'] = htmlentities($_POST['email']); 
     header("Location: profile.php"); 
     die(); 
    } 
} 
+1

你調用'使用fetchall()'兩次,也沒用(也許它會導致意想不到的行爲太) – Uby

+1

所以清爽的使用'PDO看到有人'而不是'mysql *' – asprin

回答

2

您應該添加一個默認return false;聲明:

function valid_credientials($email,$password){ 
    global $db; 

    $q = "SELECT * FROM user WHERE email = :email AND password =:password"; 
    $query = $db->prepare($q); 
    $query->execute(array(":email" => $email, ":password" => $password)); 
    $results = $query->fetchAll(); 

    if($results !=FALSE && $query->rowCount() > 0) { 
     if($results[0]['password'] == $password){ 
      $_SESSION['email'] = $email; 
      return $query->fetchAll($q,0)==1 ? true:false; 
     } 
    } 

    // return false by default 
    return false; 
} 

順便說一句,代碼縮進是你的朋友;)

然而,這可以優化更穩定:

function valid_credientials($email,$password){ 
    global $db; 

    $q = "SELECT * FROM user WHERE email = :email AND password =:password"; 
    $query = $db->prepare($q); 
    if(!$query) { 
     throw new Exception('Failed to prepare the query'); 
    } 

    $ret = $query->execute(array(":email" => $email, ":password" => $password)); 
    if(!$ret) { 
     throw new Exception('Failed to execute the query'); 
    } 

    $results = $query->fetchAll(); 
    if($results === FALSE) { 
     throw new Exception('Failed to fetch results'); 
    } 

    if(count($results) > 0) { 
     // additional password check is not necessary 
     return true; 
    } 

    // return false by default 
    return false; 
} 
+0

好吧,我試過返回false,但我仍然回到我的登錄表單:/ – Dymond

+1

然後還有其他錯誤。需要看到整個解決方案 – hek2mgl

+0

我更新了我的代碼,與isset,我一直堅持這個充足的小時.garh :) – Dymond

0

說不上爲什麼你寫了這麼多的代碼

function valid_credientials($email,$password){ 
    global $db; 

    $q = "SELECT 1 FROM user WHERE email = ? AND password = ?"; 
    $query = $db->prepare($q); 
    $query->execute(func_get_args()); 
    return $query->fetchColumn(); 
} 
+0

'fetchColumn()'=>返回結果集的下一行中的單個列,如果沒有更多行,則返回FALSE。希望OP不會通過==或===檢查爲真 –