关于使用 magic_quotes off 防止 SQL 和使用 htmlentities 防止 XSS 的问题

发布于 2024-12-01 14:16:06 字数 616 浏览 2 评论 0 原文

在我的服务器上,我关闭了 magic_quotes 。 当用户从表单将内容作为文章保存到我的数据库中时,我使用

$text = mysql_real_escape_string($_POST['text']); 来防止 SQL 注入。

这是我的输入 这就是它保存在数据库中的内容

当我 echo htmlentities($row['text']); 时,我得到 打印在屏幕上,在查看源代码时我得到 .

我的问题是

  1. 是否应该像 一样保存在数据库中以防止 SQL 注入?
  2. htmlentities 是防止 XSS 攻击的良好候选者吗?
  3. 我应该打开 magic_quotes 吗?

On my server I have magic_quotes turned off.
When a user save content as article in my DB from a form, I use

$text = mysql_real_escape_string($_POST['text']); to prevent SQL Injecion.

This is my input <img src="image.png"></img> and this is what it is saved in the DB <img src="image.png"></img>

When I echo htmlentities($row['text']); i get <img src="image.png"></img> printed on screen, on view source I get <img src="image.png"></img>.

My questions are

  1. Isn't supposed to be saved in DB like <img src=\"image.png\"></img> to prevent SQL Injections ?
  2. Is htmlentities is a good candidate to prevent XSS attacks?
  3. Should I turn on magic_quotes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

匿名的好友 2024-12-08 14:16:06

不应该像那样保存在数据库中以防止SQL注入吗?

不,SQL 注入被广泛误解,主要是因为它们实际上与 SQL 无关,因为它们只是字符串操作。您不需要更改插入数据库的数据,只需更改作为查询发送到数据库服务器的字符串(除非您做出明智的选择并使用准备好的语句而不是转义查询字符串)。数据一旦存储,就应该处于其原始状态。

htmlentities 是防止 XSS 攻击的良好候选者吗?

是的,但是 htmlentities() 适合将数据作为输出发送到浏览器,而不适合将其存储到数据库中(因为数据库中的数据可能用于网页以外的其他用途)。

我应该打开 magic_quotes 吗?

不,您应该使用准备好的语句。

Isn't supposed to be saved in DB like <img src=\"image.png\"></img> to prevent SQL Injections ?

No, SQL injections are widely misunderstood, mainly because they actually have nothing to do with SQL as they are just string manipulation. You don't need to alter the data you insert into the database, you only have to alter the string you send to the database server as query (unless you do the wise choice and use prepared statements instead of escaping the query string). The data, once stored, should be in its original state.

Is htmlentities is a good candidate to prevent XSS attacks?

Yes but htmlentities() is good for sending data as output to the browser, not for storing it into the database (as the data from the DB might be used for something other than a web page).

Should I turn on magic_quotes?

No, you should use prepared statements.

意中人 2024-12-08 14:16:06
  1. 看来您的魔术引号已启用。检查一下。
  2. 有很多关于此的文章,但为了快速入门,不允许使用 JavaScript 和外部图像。
  1. It seems that your magic quotes is enabled. Check it.
  2. There are a lot of articles about this but for quick starting don't allow to use javascript and external images.
你的往事 2024-12-08 14:16:06

从数据库中获取转义数据表明它已被双重转义 - 您的 PHP 是否打开了 magic_quotes_gpc ?如果您想清理 HTML 并仅允许您指定的某些结构,那么我建议使用 HTMLPurifier ,它会变得严格或随心所欲地放松。

Getting escaped data out of the database suggests it's been double-escaped - does your PHP have magic_quotes_gpc turned on? If you want to sanitize HTML and allow only certain constructs you specify through, then I suggest using HTMLPurifier which'll get as strict or lax as you want.

忱杏 2024-12-08 14:16:06

使用 mysql_real_escape_string 并将引号恢复正常:

$text = str_replace('\"', '"', $row['text']); // Alternative one
$text = preg_replace("/X/", '"', $row['text']); // Alternative two. X needs to be \\", \\\" or \\\\", perhaps \\\\\"

更新问题的答案:

正确保存数据如下:

输入 -> php-> mysql_real_escape_string ->;数据库-> php-> htmlspecialchars ->;浏览器

Use mysql_real_escape_string and turn the quotes back to normal:

$text = str_replace('\"', '"', $row['text']); // Alternative one
$text = preg_replace("/X/", '"', $row['text']); // Alternative two. X needs to be \\", \\\" or \\\\", perhaps \\\\\"

Answers to updated questions:

Correct saving of data goes like this:

input -> php - > mysql_real_escape_string -> db -> php -> htmlspecialchars -> browser

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文