2013-02-09 111 views
0

我一直在開發近3周以後我自己的社交網絡,我使用phpass使用下面的代碼片段湊了密碼到存儲到我的數據庫......Phpass散列密碼檢查

// END FORM DATA ERROR HANDLING 
// Begin Insertion of data into the database 

require("php_includes/PasswordHash.php"); 
$hasher = new PasswordHash(8,false); 
$p_hash = $hasher->HashPassword($p); 
if (strlen($p_hash)>=20){ 

    // Add user info into the database table for the main site table 
    $sql = "INSERT INTO users (username, email, password, gender, country, ip, signup, lastlogin, notescheck)  
      VALUES('$u','$e','$p_hash','$g','$c','$ip',now(),now(),now())"; 
    $query = mysqli_query($db_conx, $sql); 
    $uid = mysqli_insert_id($db_conx); 

小問題我已經是我的登錄頁面,在那裏我有一個簡單的表格,並在我的網頁上這下面的代碼塊...

include_once("php_includes/check_login_status.php"); 
// If user is already logged in, header that weenis away 
if($user_ok == true){ 
    header("location: user.php?u=".$_SESSION["username"]); 
    exit(); 
} 
// AJAX CALLS THIS LOGIN CODE TO EXECUTE 
if(isset($_POST["e"])){ 
    // CONNECT TO THE DATABASE 
    include_once("php_includes/db_conx.php"); 
    // GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE 
    $e = mysqli_real_escape_string($db_conx, $_POST['e']); 
    require('php_includes/PasswordHash.php'); 
    $hasher = new PasswordHash(8, FALSE); 
    $hash = $hasher->HashPassword($p); 
    $checked = $hasher->CheckPassword($p, $hash); 
    $p = $_POST["p"]; 
    // GET USER IP ADDRESS 
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR')); 
    // FORM DATA ERROR HANDLING 
    if($e == "" || $p == ""){ 
     echo "login_failed"; 
     exit(); 
    } else { 
    // END FORM DATA ERROR HANDLING 
     $sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1"; 
     $query = mysqli_query($db_conx, $sql); 
     $row = mysqli_fetch_row($query); 
     $db_id = $row[0]; 
     $db_username = $row[1]; 
     $db_pass_str = $row[2]; 
     if($p != $db_pass_str){ 
      echo "login_failed"; 
      exit(); 
     } else { 
      // CREATE THEIR SESSIONS AND COOKIES 
      $_SESSION['userid'] = $db_id; 
      $_SESSION['username'] = $db_username; 
      $_SESSION['password'] = $db_pass_str; 
      setcookie("id", $db_id, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("user", $db_username, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("pass", $db_pass_str, strtotime('+30 days'), "/", "", "", TRUE); 
      // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS 
      $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1"; 
      $query = mysqli_query($db_conx, $sql); 
      echo $db_username; 
      exit(); 
     } 
    } 
    exit(); 
} 

基本上什麼發生的事情是努力當我收到一條錯誤信息登錄!任何人有任何想法?

乾杯的答案已經channged起來像這樣...

?><?php 
// AJAX CALLS THIS LOGIN CODE TO EXECUTE 
if(isset($_POST["e"])){ 
    // CONNECT TO THE DATABASE 
    include_once("php_includes/db_conx.php"); 
    // GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE 
    $e = mysqli_real_escape_string($db_conx, $_POST['e']); 
    require('php_includes/PasswordHash.php'); 
    $hasher = new PasswordHash(8, FALSE); 
    $hash = $hasher->HashPassword($p); 
    $checked = $hasher->CheckPassword($p, $hash); 
    $hash = $_POST["p"]; 
    // GET USER IP ADDRESS 
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR')); 
    // FORM DATA ERROR HANDLING 
    if($hash != $db_pass_str){//and not $p != $dp_pass_str 
     echo "login_failed"; 
     exit(); 
    } else { 
    // END FORM DATA ERROR HANDLING 
     $sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1"; 
     $query = mysqli_query($db_conx, $sql); 
     $row = mysqli_fetch_row($query); 
     $db_id = $row[0]; 
     $db_username = $row[1]; 
     $db_pass_str = $row[2]; 
      // CREATE THEIR SESSIONS AND COOKIES 
      $_SESSION['userid'] = $db_id; 
      $_SESSION['username'] = $db_username; 
      $_SESSION['password'] = $db_pass_str; 
      setcookie("id", $db_id, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("user", $db_username, strtotime('+30 days'), "/", "", "", TRUE); 
      setcookie("pass", $db_pass_str, strtotime('+30 days'), "/", "", "", TRUE); 
      // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS 
      $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1"; 
      $query = mysqli_query($db_conx, $sql); 
      echo $db_username; 
      exit(); 
     } 
    exit(); 
} 
?> 

,但我仍然得到一個登錄錯誤

+0

什麼錯誤信息?數據庫錯誤?或只是你的「Login_failed」? – 2013-02-09 11:41:54

+0

只是一個登錄失敗的錯誤消息! – 2013-02-09 11:42:55

+0

通過您的代碼瀏覽,我只看到$ p設置爲用戶發佈的密碼,您將其與db中的假設爲_hashed_的密碼進行比較 – 2013-02-09 11:44:13

回答

1

你設置$ P作爲POST密碼(非散列)

$p = $_POST["p"]; 

看起來應該然後就:

if($hash != $db_pass_str){   //and not $p != $dp_pass_str 
    echo "login_failed"; 

即比較存儲的密碼與輸入的密碼和散列(這樣比較兩個散列)

+0

已將其更改,但仍然沒有快樂!感謝您的幫助Damien! – 2013-02-09 12:01:58