一、用到的相关技术关键词:PHP,Apache, mod_rewrite(RewriteCond,RewriteRule)地址重写, ob系列函数缓冲 file_put_contents生成html 二、流程:用户发出请求url?id=x,判断文章是否存在 (1)存在则直接转到对应的Html页面。 (2)不存在通过php读取数据库数据,然后生成html文件,并存放到指定目录。 三、实现方法: (1)地址重写用Apahce的mod_rewrite模块中的RewriteRule指令实现重写(mod_rewrite的开启和简单规则见本博另一篇http://hi.baidu.com/alex%5Fwang5...0346ffb3fb952e.html)。 (2)判断文章是否存在用Apahce的mod_rewrite模块中的RewriteCond指令 (3)生成html文件: ob_star()打开缓冲,将读取文章的php包含进来,然后用file_put_contents将获得的缓冲内容写入指定HTMl文件。 四、代码 /Test目录下的.htaccess文件内容: RewriteEngineOn RewriteRule^index.html$/news.php[L] RewriteCond%{REQUESTFILENAME}!-s RewriteRule^html/news([0-9]+).html$getnews.php?id=$1[L] 对news.php的访问将通过localhost/Test/index.html实现由第二句RewriteRule^index.html$Test/news.php[L]实现 news.php=============================>news.php将列出文章标题链接。 <div class="codetitle"><a style="CURSOR: pointer" data="28236" class="copybut" id="copybut28236" onclick="doCopy('code28236')"> 代码如下:<div class="codebody" id="code28236"> <?php header("Content-Type:text/html;charset=gbk");//以防出现乱码 mysql_connect("localhost","root",""); mysql_query('SETNAMESgbk');//我的数据库用的gbk编码,请根据自己实际情况调整 mysql_select_db("test"); $sql="Selectid ,title FROMarc orderbyid DESC"; $rs=mysql_query($sql); while($row=mysql_fetcharray($rs)){ echo"<ahref='/Test/html/news$row[id].html'>$row[title] "; } ?>
比如生成了<ahref='/Test/html/news_3.html'>php静态页实现 当点击链接发出对http://localhost/Test/html/news_3.html的请求时 Apache将会判断news_3.html是否存在,由.htaccess中的第三句 RewriteCond%{REQUEST_FILENAME}!-s 实现: RewriteCond是“定向重写发生条件”。REQUEST_FILENAME这个参数是“客户端请求的文件名” '-s'(是一个非空的常规文件[size])测试指定文件是否存在而且是一个尺寸大于0的常规的文件.!表示匹配条件的反转。 所以RewriteCond这句就表示当请求链接不存在时执行下面的RewriteRule规则。 所以当请求的news_3.html不存在时会重写地址让getnews.php?id=3来处理(否则如果news_3.html存在则直接就加载该html文件)。 getnews.php===================>功能:判断参数传输的完整性,并调用相应文件生成html文件。 <div class="codetitle"><a style="CURSOR: pointer" data="19950" class="copybut" id="copybut19950" onclick="doCopy('code19950')"> 代码如下:<div class="codebody" id="code19950"> <?php $id=$_GET['id']; $root=&$_SERVER['DOCUMENTROOT']; $filename="news".$id.".html"; $file=$root."/Test/html/".$filename; ob_start(); include($root."/Test/newsDetail.php"); file_put_contents($file,ob_get_contents()); ob_end_flush(); ?>
newsDetail.php====================>从数据库中读取数据,产生新闻内容,内容被getnews.php捕获 <div class="codetitle"><a style="CURSOR: pointer" data="97923" class="copybut" id="copybut97923" onclick="doCopy('code97923')"> 代码如下:<div class="codebody" id="code97923"> <?php header("Content-Type:text/html;charset=gbk"); if(isset($_GET['id'])){ $id=&$_GET['id']; }else{ header("Location:http://127.0.0.1/lean/Test/html/news_failed.html"); exit(); } mysql_connect("localhost",""); mysql_query('SETNAMESgbk'); mysql_select_db("test"); $id=$_GET['id']; $sql="Selectnews FROMarc Whereid =$id"; $rs=mysql_query($sql); while($row=mysql_fetch_array($rs)){ echo$row['news']; } ?>
这样将会在/Test/html目录下产生以news_文章ID.html命名的html文件。 PS:一开始在判断是否存在相应html页面时采用的是php内置的file_exists()判断,而不用Apache的RewriteCond,也即没有RewriteCond%{REQUEST_FILENAME}!-s。看似可行,但结果会产生“循环重定向”的问题。 当news_3.html不存在时我们需要用getnews.php生成news_3.html,生成完毕后需要转向到news_3.html,于是又形成了一次请求mod_rewrite又启动把news_3.html重写为getnews.php?id=3这就形成了死循环了。所以把文件存在性的判断交给RewriteCond,指定的html文件不存在时才启用重写规则。这样循环重定向的问题就没有了。 一开始没有采用fopen打开newsDetail.php,然后再将生成的内容fwrite成html文件,然后include输出静态页面。后来在fhjr999的提醒下,改为:将newDetail.php包含进getnews.php,通过ob系列函数将生成的内容放入缓冲,然后再生成html文件。ob的效率是前者的20倍左右。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|