如何允许某些HTML标签? [英] How to allow certain html tags?
问题描述
我有一个textarea,用户可以在其中撰写文章。文章可以包含文字(粗体和斜体),链接和YouTube视频。我怎样才能允许这些特定的html标签,并且仍然发布安全的防xss代码? 我会使用 HTMLPurifier ,以确保您只保留HTML
- 这是有效的
- ,并且只包含您选择允许的标签和属性
我应该补充一点,PHP提供了 strip_tags()
函数,但并不是那么好(quoting):
因为
strip_tags()
实际上并未验证HTML,所以部分或
标记可能会导致删除更多的文本/数据超出预期。
此函数不会修改您允许使用allowable_tags
的
标签上的任何属性,包括style和onmouseover
属性是恶作剧的用户在发布文本时会滥用,
将显示给其他用户。
I got a textarea where the user can write an article. The article can contain text (bold and italic), links and youtube videos. How do I allow those certain html tags and still post secure xss-preventing code?
I would use HTMLPurifier, to ensure that you only keep HTML
- That is valid
- and only contains tags and attributes you've choosen to allow
I should add that PHP provides the strip_tags()
function, but it's not that good (quoting) :
Because
strip_tags()
does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected.
This function does not modify any attributes on the tags that you allow usingallowable_tags
, including the style and onmouseover attributes that a mischievous user may abuse when posting text that will be shown to other users.
这篇关于如何允许某些HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!