简单静态页生成 过程
一直用smarty的cache,但感觉还是要自己做一个,才有感觉。网上有很多牛人的功能比较完备,打算先自己搞简单的再慢慢丰满。这两天做了一个比较简单的,在hi.baidu.net/alex_wang58记录一下。
一、用到的相关技术关键词:PHP, Apache,
mod_rewrite (RewriteCond,RewriteRule)地址重写,
ob系列函数缓冲
file_put_contents生成html
二、流程:用户发出请求url?id=x ,判断文章是否存在
(1)存在则直接转到对应的Html页面。
(2)不存在通过php读取数据库数据,然后生成html文件,并存放到指定目录。
三、实现方法:
(1)地址重写用Apahce的mod_rewrite模块中的RewriteRule指令实现重写(mod_rewrite的开启和简单规则见本博另一篇http://hi.baidu.com/alex%5Fwang5 ... 0346ffb3fb952e.html )。
(2)判断文章是否存在用Apahce 的mod_rewrite模块中的RewriteCond指令
(3)生成html文件:
ob_star()打开缓冲,将读取文章的php包含进来,然后用file_put_contents将获得的缓冲内容写入指定HTMl文件。
四、代码
/Test 目录下的 .htaccess 文件内容:
RewriteEngine On
RewriteRule ^index.html$ /news.php [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^html/news_([0-9]+).html$ getnews.php?id=$1 [L]
对news.php的访问将通过 localhost/Test/index.html 实现由第二句 RewriteRule ^index.html$ Test/news.php [L] 实现
news.php =============================> news.php将列出文章标题链接。
header("Content-Type:text/html; charset=gbk"); //以防出现乱码
mysql_connect("localhost","root","");
mysql_query('SET NAMES gbk'); //我的数据库用的gbk编码,请根据自己实际情况调整
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]
";
}
?>
比如生成了php静态页实现
当点击链接发出对 http://localhost/Test/html/news_3.html 的请求时
Apache将会判断 news_3.html 是否存在,由 .htaccess中的第三句
RewriteCond %{REQUEST_FILENAME} !-s
实现:
RewriteCond 是“定向重写发生条件”。REQUEST_FILENAME 这个参数是“客户端请求的文件名”
'-s' (是一个非空的常规文件[size]) 测试指定文件是否存在而且是一个尺寸大于0的常规的文件. !表示匹配条件的反转。
所以 RewriteCond 这句就表示当请求链接不存在时 执行下面的 RewriteRule 规则。
所以当请求的news_3.html 不存在时会重写地址让 getnews.php?id=3 来处理(否则如果news_3.html 存在则直接就加载该html文件)。
getnews.php ===================>功能:判断参数传输的完整性,并调用相应文件生成html文件。
$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 ====================> 从数据库中读取数据,产生新闻内容,内容被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'];
}
?>
这样将会在/Test/html 目录下产生以 news_文章ID.html 命名的html文件。
PS: 一开始在判断是否存在相应html页面时采用的是 php 内置的 file_exists() 判断,而不用Apache的 RewriteCond,也即没有 RewriteCond %{REQUEST_FILENAME} !-s。看似可行,但结果会产生“循环重定向”的问题。
当news_3.html 不存在时 我们需要用 getnews.php生成news_3.html ,生成完毕后需要转向到 news_3.html ,于是又形成了一次请求mod_rewrite又启动把 news_3.html重写为 getnews.php?id=3 这就形成了死循环了。所以把文件存在性的判断交给 RewriteCond ,指定的html文件不存在时才启用重写规则。这样循环重定向的问题就没有了。
一开始没有采用fopen打开newsDetail.php,然后再将生成的内容fwrite成html文件,然后include输出静态页面。后来在fhjr999的提醒下,改为:将newDetail.php包含进getnews.php,通过ob系列函数将生成的内容放入缓冲,然后再生成html文件。ob的效率是前者的20倍左右。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

How to write a simple student performance report generator using Java? Student Performance Report Generator is a tool that helps teachers or educators quickly generate student performance reports. This article will introduce how to use Java to write a simple student performance report generator. First, we need to define the student object and student grade object. The student object contains basic information such as the student's name and student number, while the student score object contains information such as the student's subject scores and average grade. The following is the definition of a simple student object: public

How to write a simple online reservation system through PHP. With the popularity of the Internet and users' pursuit of convenience, online reservation systems are becoming more and more popular. Whether it is a restaurant, hospital, beauty salon or other service industry, a simple online reservation system can improve efficiency and provide users with a better service experience. This article will introduce how to use PHP to write a simple online reservation system and provide specific code examples. Create database and tables First, we need to create a database to store reservation information. In MyS

What to do if word table of contents is generated incorrectly. With the development of technology, electronic documents have become an indispensable part of our daily work and study. When editing electronic documents, especially long articles or papers, the generation of a table of contents is a very important step. The table of contents can make it easier for readers to find the content and structure of the article and improve reading efficiency. However, sometimes we encounter some problems in the process of generating the catalog, such as catalog generation errors, disordered order, etc. So, if the word directory is generated incorrectly, how should we solve it? head
