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

PHP simple static page generation process_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:53:01835browse

1. Related technical keywords used: PHP, Apache,
Ob series function buffer
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 reading database data through PHP, then generates HTML files and stores it to the specified directory.

3. Implementation method:
(1) Address rewriting uses the RewriteRule instruction in Apahce’s mod_rewrite module to implement rewriting (for the enablement and simple rules of mod_rewrite, see another article on this blog http://hi .baidu.com/alex%5Fwang5...0346ffb3fb952e.html).
(2) Determine whether the article exists. Use the RewriteCond instruction in Apahce's mod_rewrite module.
(3) Generate the html file:
Write the obtained buffer content to the specified HTMl file.
4. Contents of the .htaccess file in the code


/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]

for news.php Access 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.



Copy code

The code is as follows: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 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 "
";
}
?>


For example,
php static page implementation
When a link is clicked to make a request for http://localhost/Test/html/news_3.html
Apache will determine whether news_3.html Exists, implemented by the third sentence in .htaccess
RewriteCond %{REQUEST_FILENAME} !-s
:

RewriteCond is the "directed rewrite occurrence condition". REQUEST_FILENAME This parameter is "the file name requested by the client"
'-s' (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.



Copy code
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 and generate news content, which is captured by getnews.php
Copy code Code 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 `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, when judging whether the corresponding html page exists, 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 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 is formed. mod_rewrite starts to rewrite news_3.html into getnews.php. ?id=3 This forms an infinite 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.
We did not use fopen to open newsDetail.php at first, 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/318838.htmlTechArticle1. Related technical keywords used: PHP, Apache, mod_rewrite (RewriteCond, RewriteRule) address rewriting, The ob series function buffers file_put_contents to generate html. 2. Process: User sends...
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