gpt4 book ai didi

用于注册和登录的 php webservice

转载 作者:行者123 更新时间:2023-11-29 12:13:48 24 4
gpt4 key购买 nike

我正在尝试用 PHP 创建一个 Web 服务,以便能够注册和登录 Android 应用程序。我能够注册成功,但是登录给我带来了困难,似乎原因是某些变量未成功获取。

这是出现问题的函数:

public function getUserByEmailAndPassword($email, $password) {
print("Getting user by email and password");
// execute query and store all results
$stmt = $this->dbh->prepare("SELECT * FROM users WHERE email = '$email'");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

print_r(" result = ");
print_r($result);

// check the results
$no_of_rows = count($result);
if ($no_of_rows > 0) {
print(" results is more than 0");
$salt = $result['salt'];
print(" salt = ");
print($salt);
echo $salt;
$encrypted_password = $result['encrypted_password'];
print(" encrypted password = ");
print($encrypted_password);
$hash = $this->checkhashSSHA($salt, $password);
print(" hash = ");
print($hash);

// check if passwords match
if($encrypted_password == $hash) {
print_r(" encrypted password matches hash");
// user authentication succeeded, return results
return $result;
}
print_r(" passwords do not match");
}
else {
print_r(" user not found");
// user not found
return false;
}
}

postman 输出返回:

Getting user by email and password   result = Array
(
[0] => Array
(
[uid] => 9
[unique_id] => 5552f5e4832e31.64783784
[name] => blabla
[email] => blabla@gmail.com
[encrypted_password] => ikfU50Ryt9ostn1o68Kb2AtgN2E3Yzg4MjBiNjg1
[salt] => 7c8820b685
[created_at] => 2015-05-13 08:57:40
[updated_at] =>
)

)
results is more than 0 salt = encrypted password = hash = Kp� passwords do not match{"tag":"login","error":true,"error_msg":"Incorrect email or password!"} destructing DB_Functionsclosing DB connectionDestructing DB_Connectclosing DB connection

如您所见,当我尝试打印变量值时,它们是空的。这是为什么?查询确实返回结果,那么我在变量分配方面做错了什么吗?

如果变量分配不正确,那么密码检查失败是有道理的......所以我的问题是:如果我的怀疑是正确的,那么我如何正确分配这些变量?

最佳答案

你真的很接近,你的问题的解决方案是这样的:

$salt = $result[0]['salt'];
print(" salt = ");
print($salt);

原因是您有一个结果数组,但数组中只有 1 项。因为它是一个数组,所以您仍然必须选择第一项。

关于用于注册和登录的 php webservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30208495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com