2015-04-25 55 views
0

我想創建一個登錄頁面,我遇到了一些麻煩。即使我知道在我的.txt文檔中有正確的密碼,但我無法讓此代碼不返回錯誤(雖然它只是散列)。沒有SQL數據庫的PHP登錄問題

這是我的PHP文件,我不能停止獲取不返回False:

<?php 
$file1 = 'userlist.txt'; 
$file2 = 'passlist.txt'; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $user = trim($_POST["usermail"]); 
    $pass = trim($_POST["password"]); 
} 

$hashedPass = ""; 

$arr1 = file($file1); 
$arr2 = file($file2); 
$userKey = array_search($user, $arr1); 


if ($userKey != false) { 
    reset($arr2); 
    for ($x = 0; $x <= $userKey; $x++) { 
     next($arr2); 
     if ($x == $userKey) { 
      $hashedPass = current($arr2); 
     } 
    } 
    echo $hashedPass; 
} 

if (password_verify($pass, $hashedPass)) { 
    header("Location: worked.html"); //change this to direct user to market 
} 
else { 
    /*header("Location: index.html"); //change this to direct user back to login page with error prompt*/ 
    print $pass; 
    print $hashedPass; 
    echo '<br>Invalid pass.'; 
    return false; 
} 
?> 

而且,如果你能想到的任何東西我應該在我的代碼,請讓我知道。

非常感謝。

編輯:現在更新我的代碼。仍然返回False。

回答

0

由於unHashfunction,它沒有得到執行(它不是從我所能看到的)中調用,所以$hashedPass沒有被設置。將來,請嘗試添加一些調試語句(例如,在return false;之前打印出$pass$hashedPass)。

+0

所以我應該這樣做: if(password_verify($ pass,$ hashedPass.unHash())){?這不適合我。 – Daniel

+0

還有其他的問題,但我認爲如果你只是刪除'function unHash(){'和關閉'}',它們都會消失。除非你需要從某處調用'unHash'函數,否則這可能是你最好的方法。再加上那個調試語句。 –

+0

我這樣做了,它仍然返回False。 – Daniel

0

在黑暗中拍攝:您在關閉正確的登錄組合時關閉了錯誤消息並且只獲得空白頁而不是重定向?

如果是這樣的話,你可以使用下面的代碼:

<?php 
$file1 = 'userlist.txt'; 
$file2 = 'passlist.txt'; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $user = trim($_POST["usermail"]); 
    $pass = trim($_POST["password"]); 
} 

$hashedPass = ""; 

$arr1 = file($file1); 
$arr2 = file($file2); 
$userKey = array_search($user, $arr1); 


if ($userKey != false) { 
    reset($arr2); 
    for ($x = 0; $x <= $userKey; $x++) { 
     next($arr2); 
     if ($x == $userKey) { 
      $hashedPass = current($arr2); 
     } 
    } 
    // echo $hashedPass; 
} 

if (password_verify($pass, $hashedPass)) { 
    header("Location: worked.html"); //change this to direct user to market 
} 
else { 
    /*header("Location: index.html"); //change this to direct user back to login page with error prompt*/ 
    print $pass; 
    print $hashedPass; 
    echo '<br>Invalid pass.'; 
    return false; 
} 
?> 

你的代碼失敗的原因是echo語句,這是標題重定向之前執行。在標題重定向之前不允許有任何輸出。 (更多關於這個行爲:How to fix "Headers already sent" error in PHP