2011-03-04 165 views
1

我有一个问题,当我输入文本时,我可以输入HTML文本。例如“我正在输入文本”。现在这个链接显示为表单提交时的链接。有关如何防止这种情况的任何想法?防止在文本中插入html html

I am entering text <a href="xyz.com">Go to my site </a>. This is the input so when i output the data it comes out as I am entering text **Go to my site** with the hyperlink. 
+2

请详细说明代码示例 – 2011-03-04 19:00:34

+1

你打算向我们展示一些代码吗? – Prisoner 2011-03-04 19:00:47

回答

6

把字符串中htmlspecialchars()strip_tags()

而且,因为我觉得清洁用于其它目的的字符串将是抛出了下一个问题,我应该提出这个:The ultimate clean/secure function

2

你是不会轻易能够防止用户进入无标签javascript,但您可以使用

strip_tags() 

在后端删除它们。

htmlspecialchars() 

不会删除这些标签,它只会编码特殊字符。

0

通常情况下,你不想阻止这个。您想要确保它在打印时不输出HTML

做到这一点的方法是,像这样:

echo $_GET['text']; // this prints HTML links etc 
echo htmlspecialchars($_GET['text']); // this does not 
0

如果我理解正确的话,你要防止注入的HTML代码。使用htmlspecialchars()

echo htmlspecialchars($_POST['myform']); 
0

在显示输入数据之前对其进行清理。这是众所周知的Cross Site Scripting(XSS)攻击。 您可以使用htmlspecialchars()string_tags()清理数据。