【问题标题】:Why mysqli prepare statement is not sanitising the data为什么 mysqli 准备语句不清理数据
【发布时间】:2018-10-17 10:20:14
【问题描述】:

我正在使用此代码

<?php
include('connection.php');
$heading = $_POST['upd_head'];
$note =  $_POST['upd_text'];
$post_id = $_POST['up_post_id'];
$supd_head= "UPDATE posts SET heading= ?, note=? WHERE post_id=?";
$stmt_head= mysqli_stmt_init($db_conx);
if(!mysqli_stmt_prepare($stmt_head, $supd_head)){
echo "sql is not ready";
 }
else{
mysqli_stmt_bind_param($stmt_head, "sss", $heading,$note,$post_id);
mysqli_stmt_execute($stmt_head);
}

?>

当我给出这个输入时

<script>alert('hack');</script>

它按原样保存在数据库中,并在我更新数据后刷新页面时发出警报消息。为什么不先清理数据?但我认为使用prepare语句时没有使用mysqli_real_escape_string

【问题讨论】:

  • 你如何回显变量?
  • “为什么不先清理数据?” - 因为这绝对不是它的工作……?它的工作是确保您向其输入的任何数据都不会在将数据插入数据库时​​造成问题。 “并在更新数据后刷新页面时发出警告消息” - 这是因为您在将数据输出到页面时没有正确处理数据。数据库与此几乎没有关系。
  • 谢谢你的回答,现在我明白了

标签: php database mysqli


【解决方案1】:

mysqli_stmt_prepare不是为了阻止Cross Site Scripting (XXS)

这完全取决于您如何显示数据。上面的链接显示了一个示例:

echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

很多信息都可以在OWASP website上找到

【讨论】:

  • 但它保存数据库中的数据
  • 为什么它是安全的?
  • 表示安全。实际上我对 sql 注入知之甚少,这就是为什么我认为任何特殊的字符输入都可能导致 sql 注入,但使用 prepare 语句我认为删除 mysqli_real_escape_string 是安全的?
  • @nitinverma 看,我们正在谈论数据库和应用程序。数据库将其视为文本。它不会将其解释为 javascript。当您运行查询并在 php 或 html 文件中显示结果时,它会将其呈现为 javascript。到那时,您将需要像我的答案一样对其进行编码,并且链接是这样说的。
  • 表示使用mysqli prepare语句时不用担心sql注入
猜你喜欢
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2013-10-22
  • 2012-02-02
  • 2015-12-07
  • 1970-01-01
  • 2016-04-05
相关资源
最近更新 更多