我应该同时使用striptags()和htmlspecialchars()来阻止XSS吗?
问题描述:
这是否取决于输入是否将打印给用户?在我的情况下,我需要将输入返回给用户(评论和生物)。
Does this depend on if the input is going to be printed to the user? In my case I need to return the input back to the user (comments and bio).
谢谢!!!
答
htmlspecialchars()
是足以阻止XSS。
htmlspecialchars()
is enough to prevent XSS.
剥离标签删除标签,但不删除特殊字符,如或
'
,所以如果您使用 strip_tags()
还必须使用 htmlspecialchars()
。
Strip tags removes tags but not special characters like "
or '
, so if you use strip_tags()
you also have to use htmlspecialchars()
.
如果您希望用户的评论显示就像他们输入的那样,请不要使用strip_tags,只使用htmlspecialchars()。
If you want users' comments to be displayed like they typed them, don't use strip_tags, use htmlspecialchars() only.