尝试使用php和mysql登录此应用程序时,将显示应用程序中使用的Authorization failed
错误。当读取php_error.log
时,显示:
[2019年5月5日03:31:51 UTC]PHP注意:未定义索引:结果
/Applications/MAMP/htdocs/iReporter/api.php,第43行[2019年5月5日]
03:31:51 UTC]PHP警告:count():参数必须是数组或
在中实现可计数的对象
/Applications/MAMP/htdocs/iReporter/api.php第43行
登录功能
//login API
function login($user, $pass) {
// try to match a row in the "login" table for the given username and password
$result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);
//Line 43
if (count($result['result'])>0) {
// a row was found in the database for username/pass combination
// save a simple flag in the user session, so the server remembers that the user is authorized
$_SESSION['IdUser'] = $result['result'][0]['IdUser'];
// print out the JSON of the user data to the iPhone app; it looks like this:
// {IdUser:1, username: "Name"}
print json_encode($result);
//edit
var_dump($result);
} else {
// no matching username/password was found in the login table
errorJson('Authorization failed');
}
}
此应用程序用于让用户无缝登录,但在不对代码进行任何更改的情况下突然停止工作。即使是未更改的备份文件也会收到相同的错误。如何更改代码以允许用户成功登录?
更新:index.php。应用程序访问函数的主文件
switch ($_POST['command']) {
case "login":
login($_POST['username'], $_POST['password']);
break;
解决了的
更改了iOS应用程序,以便在创建错误时将密码(而不是转换后的密码)发布到数据库。
最佳答案
基于错误,您无法在result
变量中获取$result
值。要检查此项,可以使用:
if (isset($result['result']) && count($result['result']) > 0)
它首先检查值是否已设置。
你还需要调查为什么你没有从数据库中得到预期的结果。为此,您需要查看
$result
变量中返回的内容,并查找DB查询可能返回的任何错误。