2012-10-20 4 views
0

그래서 내 사이트에 암호 변경 기능을 구현하려고하는데 암호 형식 을 제출하고 페이지를 새로 고치지 않고 제출하십시오. 그래서 저는 아약스를 사용하려고합니다. 다음 PHP
AJAX 암호 변경

$('#change_Pass').submit(function(e){ 
    $.ajax({ 
     data: $(this).serialize(), // get the form data 
     type: $(this).attr('POST'), // GET or POST 
     url: $(this).attr('Private/change_password.php'), // the file to call 
     success: function(response) { // on success.. 
      $('#success_div).html(response); // update the DIV 
     }, 
     error: function(e, x, r) { // on error.. 
      $('#error_div).html(e); // update the DIV 
     } 
    }); 
    e.preventDefault(); 
}); 

그리고 :

<form id="change_Pass" action="" method="post"> 
    Current Password<input type="password" id="change_password" name="change_password"><br> 
    New Password<input type="password" id="new_password" name="new_password"><br> 
    Verify Password<input type="password" id="verify_password" name="verify_password"><br> 
    <input type="submit" value="Submit"> 
</form> 

그리고 JQuery와 : 여기 내 HTML의

<?php 
$usr = $_SESSION["username"]; 
$old_pwd = $_POST["change_password"]; 
$new_pwd = $_POST["new_password"]; 

$link = new PDO('mysql:host=*;dbname=*;charset=UTF-8','*','*'); 
$query = "SELECT * 
     FROM Conference 
     WHERE Username = :un"; 

$stmt = $link->prepare($query); 

$stmt->bindParam(':un', $usr); 
$stmt->execute(); 
$row = $stmt->fetchAll(); 

$hash = $row[0]["Password"]; 
$is_correct = Bcrypt::check($old_pwd, $hash); 
if($is_correct) { 
    $query = "UPDATE Conference 
      SET `Password`=:new_pwd 
      WHERE Username = :usr"; 

    $stmt = $link->prepare($query); 
    $stmt->bindParam(':new_pwd', $new_pwd); 
    $stmt->bindParam(':usr', $usr); 
    $stmt->execute(); 
} 

그러나 나는 몇 가지에 붙어있어.
1) 데이터를 serialize하지 않고 change_password.php에 게시하여 $_POST을 사용할 수있게하려면 어떻게해야합니까?
2) change_password가 올바르게 보이나요? 기본적으로 사람이 current password에 입력 한 내용을 데이터베이스에있는 기존 비밀번호와 대조합니다. 일치하는 경우 암호가 변경됩니다.

+0

또한 $ ('# success_div')를 만듭니다 .html (응답); // 정확한 DIV를 업데이트하십시오. –

답변

1

JS가 약간 벗어났습니다. 내 의견보기 :

$('#change_Pass').submit(function(e) { 
    var $this = $(this);     // It's a good to cache stuff 

    $.ajax({ 
     data: $this.serialize(), 
     type: $this.attr('method'),   // You want `method` here 
     url: 'Private/change_password.php', // Dunno why you used `attr` 
     success: function(response) { 
      $('#success_div').html(response); 
     }, 
     error: function(e, x, r) { 
      $('#error_div').html(e); 
     } 
    }); 

    e.preventDefault(); 
}); 

또한 암호 변경 논리가 내게 적합하지 않습니다. 당신은 Bcrypt를 사용하고 있으므로 아무런 필요가 없습니다 (그리고 절대 필요하지 않습니다) 사용자의 암호를 일반 텍스트로 저장하십시오.

암호 대신 암호 대신 Bcrypt 해시를 저장하십시오. 그것은 정말로 암호 해싱의 요점입니다.

+0

이것은 올바른 대답입니다. 그리고 현재의 암호 검사를 추가하여 더 많은 보안 문제에 대한 암호를 변경할 수 있습니다 – Abadis