Simple static page generation process

Source: Internet
Author: User
Tags html page

has been using Smarty cache, but the feeling is still to do one, only feel. Online there are a lot of cattle function is relatively complete, the intention to make their own simple and then slowly plump. These two days made a relatively simple, in the hi.baidu.net/alex_wang58 record.

First, the use of related technical keywords: PHP, Apache,
Mod_rewrite (rewritecond,rewriterule) address rewrite,
OB series function buffering
File_put_contents Generate HTML

Second, the process: the user issued a request url?id=x, to determine whether the article exists
(1) There is a direct transfer to the corresponding HTML page.
(2) does not exist through PHP to read database data, and then generate HTML files, and stored in the specified directory.

Third, the realization method:
(1) Address rewriting with APAHCE mod_rewrite module in the rewriterule instruction implementation rewrite (Mod_rewrite open and simple rules see this bo another article http://hi.baidu.com/alex%5Fwang5 ... 0346ffb3fb952e.html).
(2) To determine whether the article exists in the Apahce mod_rewrite module of the rewritecond directive
(3) Generate HTML file:
Ob_star () Opens the buffer, includes PHP that reads the article, and then writes the cached content to the specified HTML file with file_put_contents.
Four, code


The contents of the. htaccess file in the/test directory:

Rewriteengine on
Rewriterule ^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 by the localhost/test/index.html by the second sentence rewriterule ^index.html$ test/news.php [L]

news.php =============================> news.php will list the article title link.

Copy PHP content to clipboard
PHP Code:

<?php
Header ("content-type:text/html; CHARSET=GBK "); In case there is garbled
mysql_connect ("localhost", "root", "");
mysql_query (' SET NAMES gbk '); GBK code for my database, please adjust according to your own 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 "<a href= '/test/html/news_$row[id].html ' > $row [title]</a><br>";
}
?>



For example, generated <a href= '/test/html/news_3.html ' >php static page implementation </a>
When you click on the link to issue a request to http://localhost/Test/html/news_3.html
Apache will judge whether News_3.html exists, by the third sentence in the. htaccess
Rewritecond%{request_filename}!-s
Realize:

Rewritecond is a "directed rewrite occurrence condition." Request_filename This parameter is "file name requested by client"
'-S ' (is a non-empty regular file [size]) tests whether the specified file exists and is a regular file size greater than 0. ! Represents an inversion of a matching condition.
So rewritecond this means that the following Rewriterule rules are executed when the request link does not exist.

So when the requested news_3.html does not exist, the address is overridden to getnews.php?id=3 (otherwise, if news_3.html exists, the HTML file is loaded directly).

getnews.php ===================> function: To determine the integrity of parameter transmission, and call the corresponding file to generate HTML files.


Copy PHP content to clipboard
PHP Code:

<?php
$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 ====================> reads data from the database, generates news content, and content is captured by getnews.php
Copy PHP content to clipboard
PHP Code:

<?php
Header ("content-type:text/html; CHARSET=GBK ");
if (Isset ($_get[' id ')) {
$id = & $_get[' id '];
}else{
Header ("Location: [Url]http://127.0.0.1/lean/test/html/news_failed.html[/url]");
Exit ();
}
mysql_connect ("localhost", "root", "");
mysql_query (' SET NAMES gbk ');
mysql_select_db ("test");
$id =$_get[' id '];

$sql = "Select ' News ' from ' Arc ' WHERE ' id ' = $id";
$rs = mysql_query ($sql);
while ($row = Mysql_fetch_array ($rs)) {
echo $row [' News '];
}
?>



This will produce an HTML file named News_ article id.html in the/test/html directory.

PS: In the beginning to determine whether the corresponding HTML page is the use of PHP built-in file_exists (), without the rewritecond of Apache, that is, there is no Rewritecond%{request_filename}!-s. Seemingly feasible, but the result will be "circular redirection" problem.
When news_3.html does not exist we need to use getnews.php to generate news_3.html, after the generation of the need to turn to news_3.html, and then formed a request mod_rewrite and start to rewrite news_3.html to Getnews.php?id=3 this creates a cycle of death. So the judgment of the existence of the file is given to Rewritecond, and the overridden rule is enabled only if the specified HTML file does not exist. The problem with cyclic redirection is gone.
At first, fopen is not used to open the newsdetail.php, and then the generated content is fwrite into an HTML file, then include the output static page. Later, in fhjr999 's reminder, instead: include the newdetail.php in getnews.php, put the generated content in a buffer through the OB series function, and then generate an HTML file. OB is about 20 times times as efficient as the former.

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.