<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[php生成静态页面]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Sat, 23 Dec 2006 10:02:39 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	看到很多朋友在各个地方发帖问php生成静态文章系统的方法，以前曾做过这样一个系统，遂谈些看法，以供各位参考。好了，我们先回顾一些基本的概念。<br/>一，php脚本与动态页面。<br/>php脚本是一种服务器端脚本程序，可通过嵌入等方法与html文件混合，也可以类，函数封装等形式，以模板的方式对用户请求进行处理。无论以何种方式，它的基本原理是这样的。由客户端提出请求，请求某一页面　----->　web服务器引入指定相应脚本进行处理　----->　脚本被载入服务器　----->　由服务器指定的php解析器对脚本进行解析形成html语言形式　---->　将解析后的html语句以包的方式传回给浏览器。由此不难看出，在页面发送到浏览器后，php就不存在了，已被转化解析为html语句。客户请求为一动态文件，事实上并没有真正的文件存在在那里，是php解析而成相对应的页面，然后发送回浏览器。这种页面处理方式被称为“动态页面”。<br/>二，静态页面。<br/>静态页面是指在服务器端确实存在的仅含html以及js，css等客户端运行脚本的页面。它的处理方式是。由客户端提出请求，请求某一页面　---->　web服务器确认并载入某一页面　---->　web服务器将该页面以包的形式传递回浏览器。由这一过程，我们对比一下动态页面，即可方现。动态页面需由web服务器的php解析器进行解析，而且通常还需连接数据库，进行数据库存取操作，然后才能形成html语言信息包；而静态页面，无须解析，无须连接数据库，直接发送，可大大减轻服务器压力，提高服务器负载能力，大幅提供页面打开速度和网站整体打开速度。但其缺点是，不能动态地对请求进行处理，服务器上必须确实存在该文件。<br/>三，模板及模板解析。<br/>模板即尚未填充内容html文件。例如：<br/>temp.html<br/><br/><div class="code"><br/><br/>&lt;html&gt; <br/>&lt;title&gt;&#123; title &#125;&lt;/title&gt; <br/>&lt;body&gt; <br/>this is a &#123; file &#125; file’s templets <br/>&lt;/body&gt; <br/>&lt;/html&gt;<br/><br/></div><br/><br/><br/>php处理：<br/>templetest.php<br/><br/><div class="code"><br/>&lt;?php <br/>$title = &quot;落伍测试模板&quot;; <br/>$file = &quot;twomax inter test templet,&lt;br&gt;author：matrix@two_max&quot;; <br/><br/>$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;); <br/>$content = fread ($fp,filesize (&quot;temp.html&quot;)); <br/>$content .= str_replace (&quot;&#123; file &#125;&quot;,$file,$content); <br/>$content .= str_replace (&quot;&#123; title &#125;&quot;,$title,$content); <br/><br/>echo $content; <br/>?&gt; <br/></div><br/><br/>模板解析处理，即将经php脚本解析处理后得出的结果填充(content)进模板的处理过程。通常借助于模板类。目前较流行的模板解析类有phplib，smarty，fastsmarty等等。模板解析处理的原理通常为替换。也有些程序员习惯将判断，循环等处理放进模板文件中，用解析类处理，典型应用为block概念，简单来说即为一个循环处理。由php脚本指定循环次数，如何循环代入等，再由模板解析类具体实施这些操作。<br/>好了，对比过静态页面与动态页面各自的优劣，现在我们就来说说，如何用php生成静态文件。<br/>php生成静态页面并不是指php的动态解析，输出html页面，而是指用php创建html页面。同时因为html的不可写性，我们创建的html若有修改，则需删掉重新生成即可。(当然你也可以选择用正则进行修改，但个人认为那样做倒不如删掉重新生成来得快捷，有些得不偿失。)<br/>言归正传。用过php文件操作函数的php fans知道，php中有一个文件操作函数fopen，即打开文件。若文件不存在，则尝试创建。这即是php可以用来创建html文件的理论基础。只要用来存放html文件的文件夹有写权限(即权限定义0777)，即可创建文件。（针对unix系统而言，win系统无须考虑。）仍以上例为例，若我们修改最后一句，并指定在test目录下生成一个名为test.html的静态文件：<br/><br/><div class="code"><br/>&lt;?php <br/>$title = &quot;落伍测试模板&quot;; <br/>$file = &quot;twomax inter test templet,&lt;br&gt;author：matrix@two_max&quot;; <br/>$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;); <br/>$content = fread ($fp,filesize (&quot;temp.html&quot;)); <br/>$content .= str_replace (&quot;&#123;file&#125;&quot;,$file,$content); <br/>$content .= str_replace (&quot;&#123;title&#125;&quot;,$title,$content); <br/>// echo $content; <br/>$filename = &quot;test/test.html&quot;; <br/>$handle = fopen ($filename,&quot;w&quot;); //打开文件指针，创建文件 <br/>/* <br/>检查文件是否被创建且可写 <br/>*/ <br/>if (!is_writable ($filename))&#123; <br/>die (&quot;文件：&quot;.$filename.&quot;不可写，请检查其属性后重试！&quot;); <br/>&#125; <br/>if (!fwrite ($handle,$content))&#123; //将信息写入文件 <br/>die (&quot;生成文件&quot;.$filename.&quot;失败！&quot;); <br/>&#125; <br/>fclose ($handle); //关闭指针 <br/><br/>die (&quot;创建文件&quot;.$filename.&quot;成功！&quot;); <br/>?&gt;<br/></div><br/><br/>实际应用中常见问题解决方案参考：<br/>一，文章列表问题：　<br/>在数据库中创建字段，记录文件名，每生成一个文件，将自动生成的文件名存入数据库，对于推荐文章，只需指向存放静态文件的指定文件夹中的该页面即可。利用php操作处理文章列表，存为字符串，生成页面时替换此字符串即可。如，在页面中放置文章列表的表格加入标记&#123;articletable&#125;，而在php处理文件中：<br/><div class="code"><br/>&lt;?php <br/>$title = &quot;落伍测试模板&quot;; <br/>$file = &quot;twomax inter test templet,&lt;br&gt;author：matrix@two_max&quot;; <br/>$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;); <br/>$content = fread ($fp,filesize (&quot;temp.html&quot;)); <br/>$content .= str_replace (&quot;&#123;file&#125;&quot;,$file,$content); <br/>$content .= str_replace (&quot;&#123;title&#125;&quot;,$title,$content); <br/>// 生成列表开始 <br/>$list = ’’; <br/>$sql = &quot;select id,title,filename from article&quot;; <br/>$query = mysql_query ($sql); <br/>while ($result = mysql_fetch_array ($query))&#123; <br/>$list .= ’&lt;a href=’.$root.$result&#91;’filename’&#93;.’ target=_blank&gt;’.$result&#91;’title’&#93;.’&lt;/a&gt;&lt;br&gt;’; <br/>&#125; <br/>$content .= str_replace (&quot;&#123;articletable&#125;&quot;,$list,$content); <br/>//生成列表结束 <br/>// echo $content; <br/>$filename = &quot;test/test.html&quot;; <br/>$handle = fopen ($filename,&quot;w&quot;); //打开文件指针，创建文件 <br/>/* <br/>检查文件是否被创建且可写 <br/>*/ <br/>if (!is_writable ($filename))&#123; <br/>die (&quot;文件：&quot;.$filename.&quot;不可写，请检查其属性后重试！&quot;); <br/>&#125; <br/>if (!fwrite ($handle,$content))&#123; //将信息写入文件 <br/>die (&quot;生成文件&quot;.$filename.&quot;失败！&quot;); <br/>&#125; <br/><br/>fclose ($handle); //关闭指针 <br/>die (&quot;创建文件&quot;.$filename.&quot;成功！&quot;); <br/>?&gt;<br/></div><br/><br/>如我们指定分页时，每页20篇。某子频道列表内文章经数据库查询为45条，则，首先我们通过查询得到如下参数：1，总页数；2，每页篇数。第二步，for ($i = 0; $i < allpages; $i++)，页面元素获取，分析，文章生成，都在此循环中执行。不同的是，die ("创建文件".$filename."成功！";这句去掉，放到循环后的显示，因为该语句将中止程序执行。例：<br/><div class="code"><br/>&lt;?php <br/>$fp = fopen (&quot;temp.html&quot;,&quot;r&quot;); <br/>$content = fread ($fp,filesize (&quot;temp.html&quot;)); <br/>$onepage = ’20’; <br/>$sql = &quot;select id from article where channel=’$channelid’&quot;; <br/>$query = mysql_query ($sql); <br/>$num = mysql_num_rows ($query); <br/>$allpages = ceil ($num / $onepage); <br/>for ($i = 0;$i&lt;$allpages; $i++)&#123; <br/>if ($i == 0)&#123; <br/>$indexpath = &quot;index.html&quot;; <br/>&#125; else &#123; <br/>$indexpath = &quot;index_&quot;.$i.&quot;html&quot;; <br/>&#125; <br/>$start = $i * $onepage; <br/>$list = ’’; <br/>$sql_for_page = &quot;select name,filename,title from article where channel=’$channelid’ limit $start,$onepage&quot;; <br/>$query_for_page = mysql_query ($sql_for_page); <br/>while ($result = $query_for_page)&#123; <br/>$list .= ’&lt;a href=’.$root.$result&#91;’filename’&#93;.’ target=_blank&gt;’.$title.’&lt;/a&gt;&lt;br&gt;’; <br/>$content = str_replace (&quot;&#123;articletable&#125;&quot;,$list,$content); <br/>if (is_file ($indexpath))&#123; <br/>@unlink ($indexpath); //若文件已存在，则删除 <br/>&#125; <br/>$handle = fopen ($indexpath,&quot;w&quot;); //打开文件指针，创建文件 <br/>/* <br/>检查文件是否被创建且可写 <br/>*/ <br/>if (!is_writable ($indexpath))&#123; <br/>echo &quot;文件：&quot;.$indexpath.&quot;不可写，请检查其属性后重试！&quot;; //修改为echo <br/>&#125; <br/>if (!fwrite ($handle,$content))&#123; //将信息写入文件 <br/>echo &quot;生成文件&quot;.$indexpath.&quot;失败！&quot;; //修改为echo <br/>&#125; <br/>fclose ($handle); //关闭指针 <br/>&#125; <br/>fclose ($fp); <br/>die (&quot;生成分页文件完成，如生成不完全，请检查文件权限系统后重新生成！&quot;); <br/>?&gt;<br/></div><br/><br/>大致思路如此，其中如其他数据生成，数据输入输出检查，分页内容指向等可酌情在页面中加入，<br/>在实际文章系统处理过程中，还有许多问题有待考虑，与动态页面不同之处，需要注意的地方还有很多，但大致思路即是如此，其他方面可举一反三而得！<br/><br/>Tags - <a href="http://www.dzhope.com/tags/%25E7%2594%259F%25E6%2588%2590%25E9%259D%2599%25E6%2580%2581/" rel="tag">生成静态</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] php生成静态页面]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>