如何用PHP制作静态网站的模板框架

这个文档描述如何安全显示的有格式的用户输入。我们将讨论没有经过过滤的输出的危险,给出一个安全的显示格式化输出的方法。 没有过滤输出的危险如果你仅仅获得用户的输入然后显示它,你可能会破坏你的输出页面,如一些人能恶意地在他们提交的输入框中嵌入 javascript脚本: This is my comment. <script language="javascript: alert(‘Do something bad here!’)">. 这样,即使用户不是恶意的,也会破坏你的一些HTML的语句,如一个表格突然中断,或是页面显示不完整。 只显示无格式的文本这是一个最简单的解决方案,你只是将用户提交的信息显示为无格式的文本。使用htmlspecialchars()函数,将转化全部的字符为HTML的编码。如<b>将转变为&lt;b&gt;,这可以保证不会有意想不到的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=&quot;([[:graph:]]+)&quot;]’, ‘<a name="1"></a>’, $output); /* links, note we try to prevent javascript in links */ $output = str_replace(‘[link=&quot;javascript’, ‘[link=&quot; javascript’, $output); $output = ereg_replace(‘[link=&quot;([[:graph:]]+)&quot;]’, ‘<a href="1">’, $output); $output = str_replace(‘[/link]’, ‘</a>’, $output); return nl2br($output); } ?>

一些注意的地方: 记住替换自定义标记生成HTML标记字符串是在调用htmlspecialchars()函数之后,而不是在这个调用之前,否则你的艰苦的工作在调用htmlspecialchars()后将付之东流。 在经过转换之后,查找HTML代码将是替换过的,如双引号"将成为&quot; 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中正确运行的小补丁。本文下载代码中的类已经经过该补丁的修正。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » 如何用PHP制作静态网站的模板框架

86 评论

  1. order avodart 0.5mg without prescription tamsulosin us zofran brand

  2. buy deltasone 40mg sale generic viagra 50mg buy viagra 100mg without prescription

  3. purchase doxycycline online lasix 40mg us lasix 100mg tablet

  4. buy clonidine 0.1 mg for sale cheap minocycline spiriva for sale online

  5. buy prilosec 10mg pills real money slots online casinos real money

  6. order cialis without prescription tadalafil 40mg us purchase sildenafil online cheap

  7. overnight delivery for viagra sildenafil women generic cialis 20mg

  8. order ceftin 500mg generic robaxin 500mg us purchase methocarbamol

  9. sildalis brand estradiol canada order lamotrigine 200mg without prescription

  10. no deposit free spins casino live blackjack cialis prices

  11. sildenafil 100mg price nolvadex tablet purchase rhinocort pill

  12. ivermectin drug stromectol order purchase avlosulfon without prescription

  13. purchase asacol for sale astelin 10ml brand buy irbesartan 300mg online cheap

  14. pamelor 25mg without prescription panadol 500mg us paroxetine 20mg cheap

  15. order ropinirole 1mg pill cost ropinirole 1mg buy trandate 100 mg without prescription

  16. order tricor online tricor 200mg cost order sildenafil 50mg online cheap

  17. order ondansetron 4mg generic zofran uk buy trimethoprim for sale

  18. purchase avodart keflex price buy orlistat no prescription

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡