Home > Backend Development > PHP Tutorial > Implementation of generating static pages using PHP technology_PHP tutorial

Implementation of generating static pages using PHP technology_PHP tutorial

WBOY
Release: 2016-07-15 13:22:27
Original
739 people have browsed it

Let’s review some basic concepts first.

1. PHP scripts and dynamic pages.

PHP script is a server-side script program that can be mixed with HTML files through embedding and other methods, and can also be used in the form of classes, function encapsulation, etc. to process user requests in the form of templates. One way or another, the basics of it are this. The client makes a request for a certain page -----> The Web server introduces the specified corresponding script for processing -----> The script is loaded into the server -----> PHP parsing specified by the server The script is parsed by the browser to form HTML language form----> The parsed HTML statement is sent back to the browser in the form of a package. It is not difficult to see from this that after the page is sent to the browser, PHP no longer exists and has been converted and parsed into HTML statements. The client request is a dynamic file. In fact, there is no real file there. PHP parses it into the corresponding page and then sends it back to the browser. This way of handling pages is called "dynamic pages".

2. Static page.

Static pages refer to pages that actually exist on the server side and only contain HTML and JS, CSS and other client-side scripts. The way it is handled is. The client makes a request for a certain page - --- > The WEB server confirms and loads a certain page - - - > The WEB server passes the page back to the browser in the form of a package. From this process, we can compare the dynamic pages and we can see. Dynamic pages need to be parsed by the PHP parser of the web server, and usually need to connect to the database and perform database access operations before they can form an HTML language information package; while static pages do not need to be parsed or connected to the database and can be sent directly, which can greatly Reduce server pressure, improve server load capacity, and greatly improve page opening speed and overall website opening speed. But its disadvantage is that the request cannot be processed dynamically, and the file must actually exist on the server.

3. Templates and template analysis.

The template means that the content html file has not yet been filled in. For example:

temp.html

Code:

The following is a quote fragment:

< ;TITLE>{ title }

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

PHP processing:

The following is a quote fragment:

templatetest.php

Code:

 $title = "HP enthusiast test template";

 $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;

 ?>

Template parsing processing is the process of filling (content) the results obtained after PHP script parsing and processing into the template. Usually with the help of template classes. Currently, the more popular template parsing classes include phplib, smarty, fastsmarty and so on. The principle of template parsing processing is usually replacement. There are also some programmers who are accustomed to putting judgment, looping and other processing into template files and processing them with parsing classes. The typical application is the block concept, which is simply a loop processing. The PHP script specifies the number of loops, how to loop through, etc., and then the template parsing class implements these operations.

Ok, after comparing the advantages and disadvantages of static pages and dynamic pages, now let’s talk about how to use PHP to generate static files.

Generating static pages with PHP does not refer to PHP’s dynamic parsing and outputting HTML pages, but refers to using PHP to create HTML pages. At the same time, because HTML is not writable, if the HTML we create is modified, it needs to be deleted and regenerated. (Of course, you can also choose to use regular rules to modify it, but I personally think that it is faster than deleting and regenerating, which is not worth the gain.)

Back to the topic. PHP fans who have used PHP file operation functions know that there is a file operation function fopen in PHP, which opens a file. If the file does not exist, try to create it. This is the theoretical basis on which PHP can be used to create HTML files. As long as the folder used to store HTML files has write permission (ie permission definition 0777), the file can be created. (For UNIX systems, Win systems do not need to be considered.) Taking the above example as an example, if we modify the last sentence and specify to generate a static file named test.html in the test directory:

Code:

The following is a quotation fragment:

$title = "TwoMax International test template";

$file = "TwoMax Inter test templet,

Author:_Max">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"); //Open the file pointer and create the file

 /*

  检查文件是否被创建且可写

  */

  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:_Max">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); //若文件已存在,则删除

  }

  $handle = fopen ($indexpath,"w"); //打开文件指针,创建文件

  /*

  检查文件是否被创建且可写

  */

  if (!is_writable ($indexpath)){

  echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo

  }

  if (!fwrite ($handle,$content)){ //将信息写入文件

  echo "生成文件".$indexpath."失败!"; //修改为echo

  }

  fclose ($handle); //关闭指针

  }

  fclose ($fp);

  die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");

  ?>

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 process, there are still many issues to be considered. There are many differences from dynamic pages that need to be paid attention to. But the general idea is this, and other aspects can be drawn by analogy.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446980.htmlTechArticleLet’s review some basic concepts first. 1. PHP scripts and dynamic pages. PHP script is a server-side script program that can be mixed with HTML files through embedding and other methods, or it can be class, function...
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