php - 如何修复此登录功能
问题描述
我的 PHP 代码中有这两个函数:
function admin_checker($username,$password)
{
$username=remove_extra_in_string($username);
$password=remove_extra_in_string($password);
$q='select * from `admin` where `username`="'.$username.'" and `password`="'.$password.'"';
$result=@mysqli_query($conn, $q);
$_COOKIE['username'] = $result["username"];
$_COOKIE['password'] = $result["password"];
if(@mysqli_num_rows($result)==1)
return 1;
else
return 0;
}
function remove_extra_in_string($string)
{
$extra=array('\'','"','`','/','*',';',' ','--');
$string=str_replace($extra,'',$string);
return $string;
}
然后通过这个检查:
if(admin_checker($_COOKIE['username'],$_COOKIE['password'])==1)
{ echo "ok"; } else { echo "not ok"; }
但不知何故它没有通过,我不知道我在这里做错了什么?
解决方案
首先也是最重要的一点:永远不要将密码存储为纯文本,也永远不要将用户数据保存在 COOKIE 中。接下来 - 不要使用你自己的函数 remove_extra_in_string(),使用内置的 mysqli_real_escape_string 甚至更好 - 使用准备好的语句:https ://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
情况是,如果您以纯文本形式存储密码,您可能有更多具有相同密码和用户名的记录,它将返回值!= 1,因此它将是 FALSE。
它也可能会失败,因为您在调用 admin_checker() 时没有设置 COOKIES,因此 $username 和 $password 等于 null。
推荐阅读
- javascript - 用括号和省略号在流中键入别名
- javascript - 更改事件侦听器中的 setAttribute 不更新视图
- flutter - Flutter:BorderRadius 在 showGeneralDialog 下不起作用
- android - 匕首 2 组件中的匕首 2 模块
- node.js - how to get the count of documents with value in different fields in mongo
- php - Woocommerce 需要很长时间才能加载并抛出我的页面并出现 503 错误
- python - 如何使用 Discord.py 获取发件人 ID?
- arrays - 空手道:在一种情况下验证 json 和数组的键时遇到问题
- c++ - 如何按键对 std::list 进行排序?
- amazon-web-services - 如何在我的 Jenkins 作业中使用存储在 AWS Secrets Manager 中的用户名和密码?