Home >Backend Development >PHP Tutorial >Simple static page generation process_PHP tutorial

Simple static page generation process_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:59:151027browse

I have always used smart cache, but I feel like I still need to make one myself to feel comfortable. There are many awesome people on the Internet who have relatively complete functions. I plan to make a simple one myself first and then gradually expand it. I made a relatively simple one in the past two days and recorded it at hi.baidu.net/alex_wang58.

1. Related technical keywords used: PHP, Apache,
mod_rewrite (RewriteCond, RewriteRule) address rewriting,
Ob series function buffer
file_put_contents generates html

2. Process: The user sends a request url?id=x to determine whether the article exists
(1) If it exists, go directly to the corresponding Html page.
(2) There is no need to read database data through php, then generate html files and store them in the specified directory.

3. Implementation method:
(1) Address rewriting uses the RewriteRule instruction in Apahce's mod_rewrite module to implement rewriting (for the opening and simple rules of mod_rewrite, see another article on this blog http://hi.baidu.com/alex%5Fwang5... 0346ffb3fb952e.html ).
(2) To determine whether an article exists, use the RewriteCond instruction in Apahce’s mod_rewrite module
(3) Generate html file:
                  ob_star() opens the buffer, includes the php that reads the article, and then uses file_put_contents to write the obtained buffer content to the specified HTMl file.
4. Code


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 localhost/Test/index.html and implemented by the second sentence RewriteRule ^index.html$ Test/news.php [L]

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

header("Content-Type:text/html; charset=gbk"); //To prevent garbled characters
mysql_connect("localhost","root","");
mysql_query('SET NAMES gbk'); //The 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, php static page implementation
is generated When a link is clicked to make a request to http://localhost/Test/html/news_3.html
Apache will determine whether news_3.html exists, based on the third sentence
in .htaccess RewriteCond %{REQUEST_FILENAME} !-s
Implementation:

RewriteCond is the "condition for directed rewriting to occur". REQUEST_FILENAME This parameter is "the file name requested by the client"
'-s' (is a non-empty regular file [size]) tests whether the specified file exists and is a regular file with a size greater than 0. ! Represents the inversion of the matching condition.
So the sentence RewriteCond means that when the requested link does not exist, the following RewriteRule rules will be executed.

So when the requested news_3.html does not exist, the address will be rewritten for getnews.php?id=3 to process (otherwise, if news_3.html exists, the html file will be loaded directly).

getnews.php ===================>Function: Determine the integrity of parameter transmission, and call the corresponding file to generate an html file.

$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 and generate news content, which is captured by getnews.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 generate an html file named news_article ID.html in the /Test/html directory.

PS: Initially, PHP's built-in file_exists() judgment was used to determine whether the corresponding html page existed, instead of Apache's RewriteCond, that is, there was no RewriteCond %{REQUEST_FILENAME} !-s. It seems feasible, but the result will be a "cyclic redirection" problem.
When news_3.html does not exist, we need to use getnews.php to generate news_3.html. After the generation is completed, we need to redirect to news_3.html, so another request mod_rewrite is initiated to rewrite news_3.html to getnews.php?id= 3 This creates an endless loop. Therefore, the judgment of file existence is handed over to RewriteCond, and the rewrite rule is enabled only when the specified html file does not exist. This way the problem of circular redirection disappears.
​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ out of fopen is not used to open newsDetail.php, and then fwrite the generated content into an html file, and then include to output the static page. Later, under the reminder of fhjr999, it was changed to: include newDetail.php into getnews.php, put the generated content into the buffer through the ob series function, and then generate the html file. The efficiency of ob is about 20 times that of the former.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631909.htmlTechArticleI have always used smarty’s cache, but I feel that I still need to make one myself to feel comfortable. There are many awesome people on the Internet who have relatively complete functions. I plan to make a simple one myself first and then gradually expand it. I did it in the past two days...
Statement:
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