Detailed explanation of generating static pages with PHP_PHP tutorial

WBOY
Release: 2016-07-21 16:11:35
Original
886 people have browsed it

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
本文出自:http://www.otm.cn 作者:Matrix@Two_Max


我们先回顾一些基本的概念。

  一,PHP脚本与动态页面。

  PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理。无论以何种方式,它的基本原理是这样的。由客户端提出请求,请求某一页面 -----> WEB服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的PHP解析器对脚本进行解析形成HTML语言形式 ----> 将解析后的HTML语句以包的方式传回给浏览器。由此不难看出,在页面发送到浏览器后,PHP就不存在了,已被转化解析为HTML语句。客户请求为一动态文件,事实上并没有真正的文件存在在那里,是PHP解析而成相对应的页面,然后发送回浏览器。这种页面处理方式被称为“动态页面”。

  二,静态页面。

  静态页面是指在服务器端确实存在的仅含HTML以及JS,CSS等客户端运行脚本的页面。它的处理方式是。由客户端提出请求,请求某一页面 ----> WEB服务器确认并载入某一页面 ----> WEB服务器将该页面以包的形式传递回浏览器。由这一过程,我们对比一下动态页面,即可方现。动态页面需由WEB服务器的PHP解析器进行解析,而且通常还需连接数据库,进行数据库存取操作,然后才能形成HTML语言信息包;而静态页面,无须解析,无须连接数据库,直接发送,可大大减轻服务器压力,提高服务器负载能力,大幅提供页面打开速度和网站整体打开速度。但其缺点是,不能动态地对请求进行处理,服务器上必须确实存在该文件。

  三,模板及模板解析。

  模板即尚未填充内容html文件。例如:

 temp.html

  Code:   

  { title }
 
     this is a { file } file's templets
 

 


PHP处理:

 templetest.php


  Code:   
  $title = "PHP爱好者测试模板";
  $file   = "TwoMax Inter test templet,
author:Sheyi";

 $fp          = fopen ("temp.html","r");
  $content  = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file }",$file,$content);
  $content .= str_replace ("{ title }",$title,$content);

  echo $content;
?>
 

  模板解析处理,即将经PHP脚本解析处理后得出的结果填充(content)进模板的处理过程。通常借助于模板类。目前较流行的模板解析类有phplib,smarty,fastsmarty等等。模板解析处理的原理通常为替换。也有些程序员习惯将判断,循环等处理放进模板文件中,用解析类处理,典型应用为block概念,简单来说即为一个循环处理。由PHP脚本指定循环次数,如何循环代入等,再由模板解析类具体实施这些操作。


  好了,对比过静态页面与动态页面各自的优劣,现在我们就来说说,如何用PHP生成静态文件。


  PHP生成静态页面并不是指PHP的动态解析,输出HTML页面,而是指用PHP创建HTML页面。同时因为HTML的不可写性,我们创建的HTML若有修改,则需删掉重新生成即可。(当然你也可以选择用正则进行修改,但个人认为那样做倒不如删掉重新生成来得快捷,有些得不偿失。)

  言归正传。用过PHP文件操作函数的PHP FANS知道,PHP中有一个文件操作函数fopen,即打开文件。若文件不存在,则尝试创建。这即是PHP可以用来创建HTML文件的理论基础。只要用来存放HTML文件的文件夹有写权限(即权限定义0777),即可创建文件。(针对UNIX系统而言,Win系统无须考虑。)仍以上例为例,若我们修改最后一句,并指定在test目录下生成一个名为test.html的静态文件:


  Code:   
  $title = "拓迈国际测试模板";
  $file   = "TwoMax Inter test templet,
author:Matrix@Two_Max";

 $fp          = fopen ("temp.html","r");
  $content  = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file }",$file,$content);
  $content .= str_replace ("{ title }",$title,$content);

  // echo $content;

  $filename = "test/test.html";
  $handle    = fopen ($filename,"w"); //打开文件指针,创建文件
  /*
 检查文件是否被创建且可写
  */
  if (!is_writable ($filename)){
     die ("文件:".$filename."不可写,请检查其属性后重试!");
  }
  if (!fwrite ($handle,$content)){  //将信息写入文件
     die ("生成文件".$filename."失败!");
  }
  fclose ($handle); //关闭指针

  die ("创建文件".$filename."成功!");
?>  
 


  实际应用中常见问题解决方案参考:

  一,文章列表问题:
  
  在数据库中创建字段,记录文件名,每生成一个文件,将自动生成的文件名存入数据库,对于推荐文章,只需指向存放静态文件的指定文件夹中的该页面即可。利用PHP操作处理文章列表,存为字符串,生成页面时替换此字符串即可。如,在页面中放置文章列表的表格加入标记{ articletable },而在PHP处理文件中:


  Code:   
  $title = "拓迈国际测试模板";
  $file   = "TwoMax Inter test templet,
author:Matrix@Two_Max";

 $fp          = fopen ("temp.html","r");
  $content  = fread ($fp,filesize ("temp.html"));
  $content .= str_replace ("{ file }",$file,$content);
  $content .= str_replace ("{ title }",$title,$content);

  //  生成列表开始
  $list = '';
  $sql = "select id,title,filename from article";
  $query = mysql_query ($sql);
  while ($result = mysql_fetch_array ($query)){
     $list .= ''.$result['title'].'
';
  }
  $content .= str_replace ("{ articletable }",$list,$content);

  //生成列表结束
  // echo $content;

  $filename = "test/test.html";
  $handle    = fopen ($filename,"w"); //打开文件指针,创建文件
  /*
 检查文件是否被创建且可写
  */
  if (!is_writable ($filename)){
     die ("文件:".$filename."不可写,请检查其属性后重试!");
  }
  if (!fwrite ($handle,$content)){  //将信息写入文件
     die ("生成文件".$filename."失败!");
  }
  fclose ($handle); //关闭指针

  die ("创建文件".$filename."成功!");
?>
 

  二,分页问题。

  如我们指定分页时,每页20篇。某子频道列表内文章经数据库查询为45条,则,首先我们通过查询得到如下参数:1,总页数;2,每页篇数。第二步,for ($i = 0; $i < allpages; $i++),页面元素获取,分析,文章生成,都在此循环中执行。不同的是,die ("创建文件".$filename."成功!";这句去掉,放到循环后的显示,因为该语句将中止程序执行。例:


Code:
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage = '20';
$sql = "select id from article where channel='$channelid'";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);

for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i."html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){
$ list .= ''.$title.'
';
}

$content = str_replace ("{ articletable }",$list,$content);

if (is_file ($indexpath)){
@unlink ($indexpath); //If the file already exists, delete it
}

$handle = fopen ($indexpath,"w"); //Open the file pointer and create the file
/*
Check whether the file is created and writable
*/
if (!is_writable ($indexpath)){
echo "File:" .$indexpath."Not writable, please check its properties and try again!"; //Modify to echo
}
if (!fwrite ($handle,$content)){ //Write information to the file
echo "generate file". $ Indexpath. "Failure!"; // Modify to echo
}
fclose ($ hand); // Close the pointer
}

fclose ($fp);
die ("Generation of paging file is completed. If the generation is incomplete, please check the file permission system and regenerate! ");

?>

The general idea is this. Other data generation, data input and output checking, paging content pointing, etc. can be added to the page as appropriate.

In the actual article system processing, there are still many issues to be considered, and there are many things that need to be paid attention to when it is different from dynamic pages, but the general idea is this, and other aspects can be learned by analogy

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313841.htmlTechArticleCopyright statement: You can reprint it at will. When reprinting, please be sure to indicate the original source and author information of the article and this article in the form of a hyperlink. Statement that this article comes from: http://www.otm.cn Author: Matrix@Two_Max I...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!