当前位置:首页 > 网络编程 > WEB编程 > PHP > 在PHP中显示格式化的用户输入

在PHP中显示格式化的用户输入

点击次数:36 次 发布日期:2008-11-18 22:08:05 作者:源代码网
源代码网推荐

源代码网整理以下你可以在这个页面下载这个文档附带的文件,也可以在文件下载中的字符处理中下载这个文档描述如何安全显示的有格式的用户输入。我们将讨论没有经过过滤的输出的危险,给出一个安全的显示格式化输出的方法。

源代码网整理以下没有过滤输出的危险

源代码网整理以下
如果你仅仅获得用户的输入然后显示它,你可能会破坏你的输出页面,如一些人能恶意地在他们提交的输入框中嵌入javascript脚本:

源代码网整理以下This is my comment.

源代码网整理以下<script language="javascript:

源代码网整理以下alert("Do something bad here!")">.

源代码网整理以下这样,即使用户不是恶意的,也会破坏你的一些HTML的语句,如一个表格突然中断,或是页面显示不完整。

源代码网整理以下只显示无格式的文本

源代码网整理以下这是一个最简单的解决方案,你只是将用户提交的信息显示为无格式的文本。使用htmlspecialchars()函数,将转化全部的字符为HTML的编码。

源代码网整理以下如<b>将转变为,这可以保证不会有意想不到的HTML标记在不适当的时候输出。

源代码网整理以下这是一个好的解决方案,如果你的用户只关注没有格式的文本内容。但是,如果你给出一些可以格式化的能力,它将更好一些。

源代码网整理以下Formatting with Custom Markup Tags

源代码网整理以下用户自己的标记作格式化

源代码网整理以下你可以提供特殊的标记给用户使用,例如,你可以允许使用...加重显示,...斜体显示,这样做简单的查找替换操作就可以了: output = str_replace("", "<b>", output);

源代码网整理以下output = str_replace("", "<i>", output);

源代码网整理以下再作的好一点,我们可以允许用户键入一些链接。例如,用户将允许输入[link="url"]...[/link],我们将转换为<a href="">...</a>语句

源代码网整理以下这时,我们不能使用一个简单的查找替换,应该使用正则表达式进行替换:

源代码网整理以下output = ereg_replace("[link="([[:graph:]] )"]", "<a href="\1">", output);

源代码网整理以下ereg_replace()的执行就是:

源代码网整理以下查找出现[link="..."]的字符串,使用<a href="..."> 替换它

源代码网整理以下[[:graph:]]的含义是任何非空字符,有关正则表达式请看相关的文章。

源代码网整理以下在outputlib.php的format_output()函数提供这些标记的转换,总体上的原则是:

源代码网整理以下调用htmlspecialchars()将HTML标记转换成特殊编码,将不该显示的HTML标记过滤掉,然后,将一系列我们自定义的标记转换相应的HTML标记。

源代码网整理以下请参看下面的源代码:

源代码网整理以下<?php

源代码网整理以下
function format_output(output) {

源代码网整理以下/****************************************************************************

源代码网整理以下* Takes a raw string (output) and formats it for output using a special

源代码网整理以下* stripped down markup that is similar to HTML

源代码网整理以下****************************************************************************/

源代码网整理以下
output = htmlspecialchars(stripslashes(output));

源代码网整理以下
/* new paragraph */

源代码网整理以下output = str_replace("[p]", "<p>", output);

源代码网整理以下
/* bold */

源代码网整理以下output = str_replace("[b]", "<b>", output);

源代码网整理以下output = str_replace("", "</b>", output);

源代码网整理以下
/* italics */

源代码网整理以下output = str_replace("[i]", "<i>", output);

源代码网整理以下output = str_replace("", "</i>", output);

源代码网整理以下
/* preformatted */

源代码网整理以下output = str_replace("[pre]", "<pre>", output);

源代码网整理以下output = str_replace("[/pre]", "</pre>", output);

源代码网整理以下
/* indented blocks (blockquote) */

源代码网整理以下output = str_replace("[indent]", "<blockquote>", output);

源代码网整理以下output = str_replace("[/indent]", "</blockquote>", output);

源代码网整理以下
/* anchors */

源代码网整理以下output = ereg_replace("[anchor="([[:graph:]] )"]", "<a name="\1"></a>", output);

源代码网整理以下
/* links, note we try to prevent javascript in links */

源代码网整理以下output = str_replace("[link="javascript", "[link=" javascript", output);

源代码网整理以下output = ereg_replace("[link="([[:graph:]] )"]", "<a href="\1">", output);

源代码网整理以下output = str_replace("[/link]", "</a>", output);

源代码网整理以下
return nl2br(output);

源代码网整理以下}

源代码网整理以下
?>

源代码网整理以下一些注意的地方:

源代码网整理以下记住替换自定义标记生成HTML标记字符串是在调用htmlspecialchars()函数之后,而不是在这个调用之前,否则你的艰苦的工作在调用htmlspecialchars()后将付之东流。

源代码网整理以下在经过转换之后,查找HTML代码将是替换过的,如双引号"将成为"

源代码网整理以下nl2br()函数将回车换行符转换为<br>标记,也要在htmlspecialchars()之后。

源代码网整理以下当转换[links=""] 到 <a href="">, 你必须确认提交者不会插入javascript脚本,一个简单的方法去更改[link="javascript 到 [link=" javascript, 这种方式将不替换,只是将原本的代码显示出来。

源代码网整理以下outputlib.php

源代码网整理以下在浏览器中调用test.php,可以看到format_output() 的使用情况

源代码网整理以下正常的HTML标记不能被使用,用下列的特殊标记替换它:

源代码网整理以下- this is bold

源代码网整理以下- this is italics

源代码网整理以下- this is [link="http://www.phpbuilder.com"]a link[/link]

源代码网整理以下- this is [anchor="test"]an anchor, and a [link="#test"]link[/link] to the anchor

源代码网整理以下[p]段落

源代码网整理以下[pre]预先格式化[/pre]

源代码网整理以下[indent]交错文本[/indent]

源代码网整理以下这些只是很少的标记,当然,你可以根据你的需求随意加入更多的标记

源代码网整理以下Conclusion

源代码网整理以下结论

源代码网整理以下这个讨论提供安全显示用户输入的方法,可以使用在下列程序中

源代码网整理以下留言板

源代码网整理以下用户建议

源代码网整理以下系统公告

源代码网整理以下BBS系统

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华