这个文档描述如何安全显示的有格式的用户输入。我们将讨论没有经过过滤的输出的危险,给出一个安全的显示格式化输出的方法。 没有过滤输出的危险如果你仅仅获得用户的输入然后显示它,你可能会破坏你的输出页面,如一些人能恶意地在他们提交的输入框中嵌入 javascript脚本: This is my comment. <script language="javascript: alert(‘Do something bad here!’)">. 这样,即使用户不是恶意的,也会破坏你的一些HTML的语句,如一个表格突然中断,或是页面显示不完整。 只显示无格式的文本这是一个最简单的解决方案,你只是将用户提交的信息显示为无格式的文本。使用htmlspecialchars()函数,将转化全部的字符为HTML的编码。如<b>将转变为<b>,这可以保证不会有意想不到的HTML标记在不适当的时候输出。这是一个好的解决方案,如果你的用户只关注没有格式的文本内容。但是,如果你给出一些可以格式化的能力,它将更好一些 Formatting with Custom Markup Tags 用户自己的标记作格式化 你可以提供特殊的标记给用户使用,例如,你可以允许使用[b]…[/b]加重显示,[i]…[/i]斜体显示,这样做简单的查找替换操作就可以了: $output = str_replace("[b]", "<b>", $output); $output = str_replace("[i]", "<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]’, ‘</b>’, $output); /* italics */ $output = str_replace(‘[i]’, ‘<i>’, $output); $output = str_replace(‘[/i]’, ‘</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 [b]bold[/b] – this is [i]italics[/i] – 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系统
v.htm’ ) ); $content = "<p>欢迎访问</p> <img src="demo.jpg"> <p>希望你能够喜欢本网站</p>"; $tpl->assign(‘CONTENT’, $content); $tpl->parse(‘HEADER’, ‘header’); $tpl->parse(‘LEFTNAV’, ‘leftnav’); $tpl->parse(‘MAIN’, ‘main’); $tpl->FastPrint(‘MAIN’); ?> 显然,这种方法有三个问题:我们必须为每一个页面复制这些复杂的、牵涉到模板的PHP代码,这与重复公共页面元素一样使得页面难以维护;现在文件又混合了HTML和PHP代码;为内容变量赋值将变得非常困难,因为我们必须处理好大量的特殊字符。 解决这个问题的关键就在于分离PHP代码和HTML内容,虽然我们不能从文件中删除所有的HTML内容,但可以移出绝大多数PHP代码。 静态网站的模板框架 首先,我们象前面一样为所有的页面公用元素以及页面整体布局编写模板文件;然后从所有的页面删除公共部分,只留下页面内容;接下来再在每个页面中加上三行PHP代码,如下所示: <?php <!– home.php –> <?php require(‘prepend.php’); ?> <?php pageStart(‘Home’); ?> <h1>你好</h1> <p>欢迎访问</p> <img src="demo.jpg"> <p>希望你能够喜欢本网站</p> <?php pageFinish(); ?> ?> 这种方法基本上解决了前面提到的各种问题。现在文件里只有三行PHP代码,而且没有任何一行代码直接涉及到模板,因此要改动这些代码的可能性极小。此外,由于HTML内容位于PHP标记之外,所以也不存在特殊字符的处理问题。我们可以很容易地将这三行PHP代码加入到所有静态HTML页面中。 require函数引入了一个PHP文件,这个文件包含了所有必需的与模板相关的PHP代码。其中pageStart函数设置模板对象以及页面标题,pageFinish函数解析模板然后生成结果发送给浏览器。 这是如何实现的呢?为什么在调用pageFinish函数之前文件中的HTML不会发送给浏览器?答案就在于PHP 4的一个新功能,这个功能允许把输出到浏览器的内容截获到缓冲区之中。让我们来看看prepend.php的具体代码:
以下为引用的内容: <?php require(‘class.FastTemplate.php’); function pageStart($title = ”) { GLOBAL $tpl; $tpl = new FastTemplate(‘.’); $tpl->define( array( ‘main’ => ‘main.htm’, ‘header’ => ‘header.htm’, ‘leftnav’=> ‘leftnav.htm’ ) ); $tpl->assign(‘TITLE’, $title); ob_start(); } function pageFinish() { GLOBAL $tpl; $content = ob_get_contents(); ob_end_clean(); $tpl->assign(‘CONTENT’, $content); $tpl->parse(‘HEADER’, ‘header’); $tpl->parse(‘LEFTNAV’, ‘leftnav’); $tpl->parse(‘MAIN’, ‘main’); $tpl->FastPrint(‘MAIN’); } ?> |
pageStart函数首先创建并设置了一个模板实例,然后启用输出缓存。此后,所有来自页面本身的HTML内容都将进入缓存。pageFinish函数取出缓存中的内容,然后在模板对象中指定这些内容,最后解析模板并输出完成后的页面。 这就是整个模板框架全部的工作过程了。首先编写包含了网站各个页面公共元素的模板,然后从所有页面中删除全部公共的页面布局代码,代之以三行永远无需改动的PHP代码;再把FastTemplate类文件和prepend.php加入到包含路径,这样你就得到了一个页面布局可以集中控制的网站,它有着更好的可靠性和可维护性,而且网站级的大范围修改也变得相当容易。 本文下载包包含了一个可运行的示例网站,它的代码注释要比前面的代码注释更详细一些。FastTemplate类可以在http://www.thewebmasters.net/找到,最新的版本号是1.1.0,那里还有一个用于保证该类在PHP 4中正确运行的小补丁。本文下载代码中的类已经经过该补丁的修正。
波比源码 » 如何用PHP制作静态网站的模板框架
buy levaquin 250mg online levaquin oral
order avodart 0.5mg without prescription tamsulosin us zofran brand
spironolactone ca finasteride 1mg oral fluconazole 200mg price
cheap ampicillin 250mg order ampicillin 500mg for sale erythromycin 500mg pill
order generic fildena 50mg sildenafil 50mg brand buy robaxin 500mg pill
order sildenafil 50mg pills order sildenafil online order estradiol 2mg online cheap
oral lamotrigine 200mg buy tretinoin online cheap retin over the counter
order tadalafil generic tadalafil order online buy voltaren 50mg sale
isotretinoin pills order accutane 10mg generic order generic azithromycin 250mg
buy indomethacin 50mg sale oral amoxicillin 250mg buy trimox sale
arimidex sale viagra online indonesia cheap sildenafil pill
buy deltasone 40mg sale generic viagra 50mg buy viagra 100mg without prescription
buy modafinil online order modafinil pills acetazolamide 250 mg drug
purchase doxycycline online lasix 40mg us lasix 100mg tablet
purchase ramipril generic order astelin 10ml without prescription buy generic astelin 10 ml
buy clonidine 0.1 mg for sale cheap minocycline spiriva for sale online
order alendronate 70mg for sale famotidine 20mg without prescription famotidine 40mg pills
benicar 20mg drug buy diamox 250 mg pill acetazolamide 250mg brand
tacrolimus 5mg tablet order tacrolimus 1mg generic generic ursodiol 300mg
buy molnupiravir for sale molnupiravir 200mg ca lansoprazole usa
purchase zoloft without prescription buy sertraline 100mg online sildenafil price
cialis 40mg pill cialis professional viagra overnight delivery
order avlosulfon 100 mg sale purchase dapsone for sale perindopril 4mg generic
buy generic provera praziquantel 600 mg pill order periactin 4mg online cheap
order provigil 100mg pill modafinil 100mg brand ivermectin for humans walmart
buy luvox 100mg without prescription order cymbalta 20mg online glucotrol price
accutane us order accutane 10mg generic prednisone generic
cost azithromycin order prednisolone 5mg for sale order generic gabapentin 600mg
generic tadalafil 10mg cialis overnight shipping canadian viagra and healthcare
lasix buy online buy doxycycline online plaquenil 200mg drug
order itraconazole 100 mg sale sporanox 100 mg canada tinidazole 500mg generic
order viagra 50mg sale order viagra online lisinopril 2.5mg drug
buy linezolid 600 mg pills buy linezolid online cheap bonus casino
buy prilosec 10mg pills real money slots online casinos real money
buy lopressor sale atenolol price vardenafil ca
help with essays buy nothing day essay buy sildenafil online
order cialis without prescription tadalafil 40mg us purchase sildenafil online cheap
buy aristocort 4mg pills buy desloratadine 5mg for sale purchase desloratadine pill
orlistat us purchase diltiazem for sale acyclovir pill
buy zyloprim 300mg generic purchase zetia online brand zetia 10mg
methotrexate 10mg over the counter order generic methotrexate 10mg reglan pill
losartan generic cozaar 25mg canada buy topiramate 200mg online
imitrex online order levaquin 250mg generic order avodart 0.5mg sale
order colchicine 0.5mg for sale purchase colchicine online cheap money slots
order simvastatin 10mg pills propecia for sale finasteride us
order metronidazole for sale buy metronidazole 400mg pills trimethoprim ca
order diflucan 200mg pills buy viagra 50mg online cheap sildenafil viagra
keflex tablet purchase keflex online cheap buy erythromycin 250mg pill
overnight delivery for viagra sildenafil women generic cialis 20mg
order ceftin 500mg generic robaxin 500mg us purchase methocarbamol
real money online casino free roulette online order cialis 10mg for sale
desyrel 50mg canada generic sildenafil 50mg aurogra brand
sildalis brand estradiol canada order lamotrigine 200mg without prescription
buy prednisone 40mg deltasone 5mg tablet amoxil 1000mg uk
no deposit free spins casino live blackjack cialis prices
real casino slots buy modafinil for sale provigil 100mg tablet
sildenafil 100mg price nolvadex tablet purchase rhinocort pill
cost tretinoin gel buy tretinoin cream generic buy avanafil 200mg generic
terbinafine ca trimox canada buy generic trimox
naprosyn 500mg cheap buy naprosyn generic cheap prevacid 15mg
biaxin 250mg cheap clonidine medication oral antivert 25mg
order montelukast 10mg for sale purchase viagra without prescription sildenafil without a doctor’s prescription
cialis sale Cialis prices money slots
ivermectin drug stromectol order purchase avlosulfon without prescription
free blackjack real casino games win real money online casino
blackjack online for real money casino games win real money roulette casino
order ramipril generic buy glimepiride pill purchase arcoxia sale
where can i buy a research paper help writing papers sulfasalazine online
purchase asacol for sale astelin 10ml brand buy irbesartan 300mg online cheap
purchase olmesartan sale order benicar 10mg for sale buy depakote sale
buy generic carvedilol 25mg oxybutynin 5mg cheap elavil buy online
pamelor 25mg without prescription panadol 500mg us paroxetine 20mg cheap
purchase doxycycline online methylprednisolone 16 mg tablet purchase medrol for sale
famotidine 20mg sale order generic prograf 1mg remeron pills
order ropinirole 1mg pill cost ropinirole 1mg buy trandate 100 mg without prescription
tadacip 10mg price amoxicillin sale cheap trimox 500mg
order tricor online tricor 200mg cost order sildenafil 50mg online cheap
brand tadalafil 20mg Original brand tadalafil viagra for men
otc ed pills sildenafil drug propecia 5mg cheap
stromectol prices stromectol for people order prednisone 5mg generic
order ondansetron 4mg generic zofran uk buy trimethoprim for sale
antihistamine tablets buy synthroid generic amoxiclav medication
buy prednisolone 40mg buy gabapentin 800mg without prescription cost lasix
order provigil pills brand lopressor 100mg metoprolol generic
order doxycycline 100mg acyclovir 800mg oral purchase zovirax online cheap
purchase avodart keflex price buy orlistat no prescription