Php simple static page generation process _ PHP Tutorial

Source: Internet
Author: User
Php simple static page generation process. I. related technical keywords used: PHP, Apache, mod_rewrite (RewriteCond, RewriteRule) address rewriting, ob series function buffer file_put_contents generate html II. process: user Development 1. related technical keywords used: PHP, Apache,
Mod_rewrite (RewriteCond, RewriteRule) address rewriting,
Ob series function buffer
File_put_contents generate html

2. process: the user sends a request url? Id = x to determine whether the article exists
(1) directly go to the corresponding Html page.
(2) the database data is not read through php, and html files are generated and stored in the specified directory.

III. implementation method:
(1) address rewriting using the Apahce mod_rewrite module in the RewriteRule command to achieve rewriting (mod_rewrite opening and simple rules see another article in this blog http://hi.baidu.com/alex%5Fwang5 .. 0366ffb3fb952e.html ).
(2) determine whether the RewriteCond instruction in the mod_rewrite module of Apahce exists.
(3) generate an html file:
Ob_star () open the buffer, include the php that reads the article, and then use file_put_contents to write the obtained buffer content to the specified HTMl file.
IV. code


The. htaccess file in the/Test Directory:

RewriteEngine On
RewriteRule into index.html $/news. php [L]
RewriteCond % {REQUEST_FILENAME }! -S
RewriteRule ^ html/news_(%0-9%%%%%.html $ getnews. php? Id = $1 [L]

Access to news. php will be implemented through localhost/Test/index.html by the second RewriteRule rule index.html $ Test/news. php [L ].

News. php =======================================> news. php will list the article title links.

The code is as follows:


Header ("Content-Type: text/html; charset = gbk"); // prevents garbled characters
Mysql_connect ("localhost", "root ","");
Mysql_query ('set NAMES gbk'); // gbk encoding used by my database. Please adjust it according to your actual situation.
Mysql_select_db ("test ");

$ SQL = "Select 'id', 'title' FROM 'arc' order by 'id' DESC ";
$ Rs = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ rs )){
Echo "$ row [title]
";
}
?>


For example, a php static page is generated.
When you click a link to send a request for http: // localhost/Test/html/news_3.html
Apache checks whether news_3.html exists.
RewriteCond % {REQUEST_FILENAME }! -S
Implementation:

RewriteCond is the condition for the occurrence of a targeted rewrite ". REQUEST_FILENAME: "file name requested by the client"
'-S' (a non-empty regular file [size]) to test whether the specified file exists and is a regular file with a size greater than 0 .! Indicates that the matching condition is reversed.
Therefore, the RewriteCond statement indicates that the following RewriteRule rule is executed when the request link does not exist.

When the requested news_3.html does not exist, the address will be rewritten to getnews. php? Id = 3 (if news_3.html exists, the html file is directly loaded ).

Getnews. php ==========================> function: judge the integrity of parameter transmission and call the corresponding file to generate an html file.

The code is as follows:


$ Id = $ _ GET ['id'];
$ Root = & $ _ SERVER ['document _ root'];
$ 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 ==========================> read data from the database, generate news content, and the content is captured by getnews. php

The code is as follows:


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", "root ","");
Mysql_query ('set NAMES gbk ');
Mysql_select_db ("test ");
$ Id = $ _ GET ['id'];

$ SQL = "Select 'new' FROM 'arc' Where 'id' = $ id ";
$ Rs = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ rs )){
Echo $ row ['news'];
}
?>


In this way, the html file named news_article id.html will be generated under the/Test/html directory.

PS: When determining whether the corresponding html page exists at the beginning, php's built-in file_exists () judgment is used instead of Apache's RewriteCond, that is, there is no RewriteCond % {REQUEST_FILENAME }! -S. It seems feasible, but the result will produce a "loop redirection" problem.
When news_3.html does not exist, we need to use getnews.phpto generate news_3.html. after the generation is complete, we need to switch to news_3.html. so we formed a request mod_rewrite and started to rewrite news_3.html to getnews. php? Id = 3, which forms an endless loop. Therefore, the file existence judgment is handed over to RewriteCond. the rewrite rule is enabled only when the specified html file does not exist. In this way, the issue of loop redirection will disappear.
At first, newsDetail. php was not opened using fopen, and fwrite the generated content into an html file. then include the output static page. Later, with the reminder of fhjr999, newDetail. php was added to getnews. php, and the generated content was buffered through ob functions, and then html files were generated. Ob is about 20 times more efficient than the former.

Rewrite, mod_rewrite (RewriteCond, RewriteRule) address rewriting, ob series function buffer file_put_contents generate html II. process: User issue...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.