PDO与清理日期/删除 HTML
我让用户使用此代码更新他们的姓名。
$dbh = connect();
$q = $dbh->prepare('UPDATE Users SET username=:name WHERE User_ID=:id LIMIT 1');
$q->bindParam(":id", $loggedInUser->user_id, PDO::PARAM_INT);
$q->bindParam(":name", $_GET['name'], PDO::PARAM_STR);
$q->execute();
A)这足以净化信息吗? b) 当我在其中添加 HTML 标签(如 name
)时,它实际上在我的网站上以粗体显示!是否有一个选项可以让 PDO 删除所有 HTML?
I'm letting users update their name with this code.
$dbh = connect();
$q = $dbh->prepare('UPDATE Users SET username=:name WHERE User_ID=:id LIMIT 1');
$q->bindParam(":id", $loggedInUser->user_id, PDO::PARAM_INT);
$q->bindParam(":name", $_GET['name'], PDO::PARAM_STR);
$q->execute();
A) is this enough to sanitize information?
b) when I put HTML tags in there like <b>name</b>
it actually shows up in bold on my site! Is there an option where I can have PDO strip out all HTML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来相当合理。不过,我建议使用 POST 而不是 GET 来进行破坏性/操纵操作。如果您坚持使用 POST 数据,那么您遭受 CSRF 攻击的可能性就会小得多,尽管这并不能让您完全免疫。
如果您实际上并不希望用户在名称字段中输入 HTML,则不必担心在进入数据库的过程中过滤数据。通过
htmlspecialchars()
或htmlentities()
将其转义。我一直坚持这样的观点:数据应该尽可能原始地存入数据库。
编辑:差点忘了,在尝试使用它们之前,请确保
$_GET
/$_POST
中的预期值确实存在,例如Looks reasonably sound. I would suggest using POST instead of GET for destructive / manipulative operations though. You're far less likely to suffer from CSRF attacks if you stick to POST data though it does not make you totally immune.
If you do not actually want users to enter HTML into the name field, don't worry about filtering data on the way into the database. Escape it on the way out via
htmlspecialchars()
orhtmlentities()
.I've always stood by the idea that data should go into the database as raw as possible.
Edit: Almost forgot, make sure the expected values in
$_GET
/$_POST
actually exist before attempting to use them, egA) 阅读手册:
B) 永远不要相信用户的数据。在输出中使用
htmlspecialchars
。C) 使用 $_POST 和令牌进行查询,这将更改任何数据,以避免 CSRF。
A) Read manual:
B) Never trust to user's data. Use
htmlspecialchars
in output.C) Use $_POST and tokens for queries, which will change any data, to avoid CSRF.
永远不要相信用户输入!至少,将
$_GET['name']
包装在清理函数中,例如 mysql_real_escape_string() 防止 SQL 注入攻击。然后,当您输出用户提供的数据时,请确保将其包装在 htmlspecialchars() 中 防止跨站脚本(XSS)攻击。Never trust user input! At a minimum, wrap the
$_GET['name']
in a sanitizing function, like mysql_real_escape_string() to prevent SQL Injection attacks. And then when you're outputting user-supplied data, make sure to wrap it in htmlspecialchars() to prevent Cross-Site Scripting (XSS) attacks.