加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 参数化查询

发布时间:2021-07-06 05:22:48 所属栏目:大数据 来源: https://www.jb51.cc
导读:请问这段代码安全吗? /* Create a new MysqLi object with database connection parameters */$MysqLi = new MysqL('localhost', 'username', 'password', 'db');if(MysqLi_connect_errno()) {echo "Connection Failed: " . MysqLi_connect_errno();exit();

请问这段代码安全吗?

/* Create a new MysqLi object with database connection parameters */
$MysqLi = new MysqL('localhost', 'username', 'password', 'db');

if(MysqLi_connect_errno()) {
echo "Connection Failed: " . MysqLi_connect_errno();
exit();
}

/* Create a prepared statement */
if($stmt = $MysqLi -> prepare("SELECT priv FROM testUsers WHERE username=?
AND password=?")) {

/* Bind parameters
s - string, b - boolean, i - int, etc */
$stmt -> bind_param("ss", $user, $pass);

/* Execute it */
$stmt -> execute();

/* Bind results */
$stmt -> bind_results($result);

/* Fetch the value */
$stmt -> fetch();

echo $user . "'s level of priviledges is " . $result;

/* Close statement */
$stmt -> close();
}

/* Close connection */
$MysqLi -> close();

解决方法:

至于防止MysqL注入的问题:是的. MysqLi的参数化查询可以安全地抵御注入攻击.

如果$user来自外部源,您可能希望添加htmlentities()echo语句以防止用户注册用户名,例如< script>(某些恶意代码)< / script>

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读