PHP summarizes my simple static page generation process,

Source: Internet
Author: User
Tags php code
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 the relevant Technology Key Words: PHPApache
Mod_rewrite (rewritecond,rewriterule) address rewrite,
OB series function buffering
File_put_contents Generate HTML

two . Process: The user makes the request Url?id=x, determines whether the article exists
(1) There is a direct transfer to the corresponding HTML page.
(2) does not exist through PHP read DatabaseData, and then generates an HTML file and stores it in the specified directory.

Three Implementation Methods:
(1) Address rewriting with Apahce mod_rewrite ModuleRewriterule 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 through the localhost/test/index.html by the second sentence Rewriterule ^index.html$ test/news.php [L] Implementation

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>" ;
}
?>


Like the <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
Implementation:

Rewritecond is "directed Override 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 The reversal of the matching condition.
So Rewritecond This means that when the request link does not exist, execute the following rewriterule rules.

So when the requested When news_3.html does not exist, it overrides the address let getnews.php?id=3 to deal with (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 getnews.php Capture
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 used in PHP built-in file_exists () judgment, rather than the Apache Rewritecond, which is not rewritecond%{request_filename}!-s . Seems feasible, but the result will be " Loop Redirection "the question.
When news_3.html when it doesn't exist, we need to use getnews.php Generation news_3.html , you need to turn to news_3.html, and then formed a request mod_rewrite and started to news_3.html rewrite for getnews.php?id=3 this forms a dead loop. So the judgment of the existence of the document is given Rewritecond to enable overriding rules if the specified HTML file does not exist. This the problem with circular redirection is gone.
It didn't start with a fopen . newsdetail.php, and then fwrite the generated content into an HTML file , and then include output static pages. Later inFhjr999 's reminder, instead: include the newdetail.php in getnews.php, the generated content is buffered by the OB series function, and then the HTML file is generated. 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.