转义数据 - stripslashes、strip_tags
为什么很多人在字符串上使用这两个函数? 我看到很多 stripslashes(strip_tags($field));
(或相反)
strip_tags
不足以过滤任何 xss 之类的东西吗?
Why do a lot of people use both these functions on a string?
I see a lot of stripslashes(strip_tags($field));
(or the other way around)
Isn't strip_tags
enough to filter any xss stuff and such things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我注意到 strip_tags() 添加反斜杠来引用字符。我检查过,magic_quotes_gpc 没有打开。 OP最初的问题是为什么有些编码员用 stripslashes() 包围 strip_tags() ,这就是我这样做的原因,因为我不希望数据库存储双反斜杠,因为无论如何在将数据保存到数据库之前我已经准备好了数据。
I have noticed that strip_tags() adds backslashes to quote characters. I have checked and magic_quotes_gpc is NOT turned on. The OP's original question was why some coders surround strip_tags() with stripslashes() and that is why I do it, because I don't want the database to store double backslashes since I already prepare my data anyway before saving it to the database.
转义数据与
strip_tags
或stripslashes
无关。这些函数从字符串中过滤掉某些字符,而“转义”则对某些字符进行编码,这样它们就不会被浏览器或数据库解释。您可以使用
strip_tags
删除从浏览器发送到 PHP 的字符串中的 HTML 标记。更好的是,如果您使用htmlspecialchars
来转义任何可能在将数据发送回时分隔标记的字符,则您还可以安全地存储相同的数据,而无需通过strip_tags
传递它。浏览器。stripslashes
从字符串中删除斜杠,并且您只需如果启用“魔术引号”,则需要担心它。这是早期的遗留问题,当时 PHP 开发人员天真地认为来自浏览器的每一条数据都注定要进入数据库,并且不能相信开发人员能够自行逃离数据库。Escaping data has nothing to do with
strip_tags
orstripslashes
. These functions filter certain characters out of a string while "escaping" encodes certain characters so they won't be interpreted by a browser or database.You can use
strip_tags
to remove HTML tags in strings being sent to PHP from the browser. Better yet, you could also safely store the same data without passing it throughstrip_tags
if you usehtmlspecialchars
to escape any characters that could delimit tags when you send the data back to the browser.stripslashes
removes slashes from a string, and you only need to worry about it if "magic quotes" are enabled. It's a hold-over from an earlier time when the PHP devs naively assumed every piece of data coming from the browser was destined for a database and that developers couldn't be trusted to escape the database themselves.没有。过滤 XSS 内容的唯一安全方法是
htmlspecialchars()
,尽管我看到许多建议另外使用strip_tags()
。请参阅此问题中的讨论: 正在防止 XSS SQL 注入就这么简单...
stripslashes
在这种情况下应该做什么,我不知道。这可能是试图消除现已弃用的 魔法引号 的影响功能 - 但在没有首先检查该特定功能是否启用的情况下,决不应该应用此功能。Nope. The only safe way to filter out XSS stuff is
htmlspecialchars()
, although I see many recommendations to usestrip_tags()
in addition.See e.g. discussion in this question: Is preventing XSS and SQL Injection as easy as does this…
What the
stripslashes
is supposed to do in this context, I have no idea. It is probably an attempt to undo the effects of the now-deprecated magic quotes function - but this should never be applied without checking first whether that particular function is enabled.当启用魔术引号时,它将自动转义所有 POST、GET 等变量中的引号。 stripslashes 在使用数据之前删除它们。剥离标签尝试删除所有 html 标签。
When magic quotes is on it will automatically escape quotes in all of the POST, GET, etc. variables. stripslashes removes those before you use the data. Strip tags tries to remove all of the html tags.
strip_tags()
通常不足以单独防止 XSS 攻击,因此最好谨慎行事。请考虑以下事项:
并不总是需要 HTML 标签来执行 XSS 攻击。这可能是一种效率较低的攻击,但它仍然是一个潜在的攻击媒介。
strip_tags()
usually isn't enough to prevent XSS attacks on it's own, so it's best to err on the side of caution.Consider the following:
One doesn't always need HTML tags to execute an XSS attack. It may be a less effective attack, but it's still a potential attack vector nonetheless.