当用户点击提交按钮时,ajax会将数据传递给php脚本以检查登录是否有效或无效。
下面,未验证密码。传递给checkLogin类的数据(电子邮件,密码)是正确的,因为其他数据可以使用电子邮件地址进行检索。仅限于
$flag=false;
if (password_verify($this->password, $hashAndSalt)) {
$flag=true;
}
它返回假。我无法发现错误。任何人都可以看到我的剧本中出了什么问题?
JS
/*login user*/
$("document").ready(function(){
$("#login-user").submit(function(){
alert("submited");
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "text",
url: "login-this-user.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
console.log(data);
alert(data);
}
});//end success
return false;
});//end form
});
PHP
<?php
session_start();
include('config.php');
include('class.login.php');
//$return = $_POST;
$return ='{"email":"jane@ymail.com","pass":"jane","action":"test"}';
//$return['json']= json_encode($return);
//
//below code to store in database
$data = json_decode($return, true);
$login = new checkLogin();
$return_value = $login->checkLogin($data["email"],$data["pass"]);
echo $return_value;
?>
检查登录的课程
<?php
class checkLogin
{
public $email;
public $password;
public $userId;
public $salt;
public $hpass;
public function __construct()
{
}
public function checkLogin($param1, $param2)
{
$this->email=$param1;
$this->password=$param2;
$sql = "SELECT *FROM agency WHERE agency_email='{$this->email}'";
$statement = connection::$pdo->prepare($sql);
$statement->execute();
while( $row = $statement->fetch()) {
echo "salt ".$salt=$row['agency_salt'];
echo "hash ".$hashAndSalt=$row['agency_pass'];
$user_id=$row['agency_id'];
}
$flag=false;
if (password_verify($this->password, $hashAndSalt)) {
$flag=true;
}
return $flag;
}
}
?>
注册用户并存储密码时的哈希:
/*....salting starts........*/
$cost = 10;
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
//$salt = sprintf("$2a$%02d$", $cost) . $salt;
$options = array('cost' => $cost,'salt' => $salt);
//$password = crypt($data['password'], $salt);
$hash = password_hash($data['passsword'], PASSWORD_DEFAULT,$options);
/*..........salting ends..............*/