Table of Contents
PHP代码:
Home php教程 PHP源码 PHP总结我的简单静态页生成 过程,

PHP总结我的简单静态页生成 过程,

Jun 08, 2016 pm 05:33 PM
html nbsp news php quot

<script>ec(2);</script>
一直用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将列出文章标题链接。
复制PHP内容到剪贴板
PHP代码:
<span style="color: #000000"><br> <font face="新宋体"><span style="color: #0000bb"><?php <br /> header</span><span style="color: #007700">(</span><span style="color: #dd0000">"Content-Type:text/html; charset=gbk"</span><span style="color: #007700">); </span></font><font face="新宋体"><span style="color: #ff8000">//以防出现乱码<br> </span><span style="color: #0000bb">mysql_connect</span><span style="color: #007700">(</span><span style="color: #dd0000">"localhost"</span><span style="color: #007700">,</span><span style="color: #dd0000">"root"</span><span style="color: #007700">,</span><span style="color: #dd0000">""</span></font><font face="新宋体"><span style="color: #007700">);<br> </span><span style="color: #0000bb">mysql_query</span><span style="color: #007700">(</span><span style="color: #dd0000">'SET NAMES gbk'</span><span style="color: #007700">); </span></font><font face="新宋体"><span style="color: #ff8000">//我的数据库用的gbk编码,请根据自己实际情况调整<br> </span><span style="color: #0000bb">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #dd0000">"test"</span></font><font face="新宋体"><span style="color: #007700">);<br> <br> </span><span style="color: #0000bb">$sql </span><span style="color: #007700">= </span><span style="color: #dd0000">"SELECT `id`,`title` FROM `arc` order by `id` DESC"</span></font><font face="新宋体"><span style="color: #007700">;<br> </span><span style="color: #0000bb">$rs </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000bb">$sql</span></font><font face="新宋体"><span style="color: #007700">);<br> while(</span><span style="color: #0000bb">$row </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000bb">$rs</span></font><font face="新宋体"><span style="color: #007700">) ){<br> echo </span><span style="color: #dd0000">"<a>$row[title]</a><br>"</span></font><font face="新宋体"><span style="color: #007700">;<br> }<br> </span><span style="color: #0000bb">?></span><br> </font></span><br>

比如生成了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文件。

复制PHP内容到剪贴板
PHP代码:
<span style="color: #000000"><br> <font face="新宋体"><span style="color: #0000bb"><?php <br /> $id </span><span style="color: #007700">=</span><span style="color: #0000bb">$_GET</span><span style="color: #007700">[</span><span style="color: #dd0000">'id'</span></font><font face="新宋体"><span style="color: #007700">];<br> </span><span style="color: #0000bb">$root </span><span style="color: #007700">=& </span><span style="color: #0000bb">$_SERVER</span><span style="color: #007700">[</span><span style="color: #dd0000">'DOCUMENT_ROOT'</span></font><font face="新宋体"><span style="color: #007700">];<br> </span><span style="color: #0000bb">$filename </span><span style="color: #007700">= </span><span style="color: #dd0000">"news_"</span><span style="color: #007700">.</span><span style="color: #0000bb">$id</span><span style="color: #007700">.</span><span style="color: #dd0000">".html"</span></font><font face="新宋体"><span style="color: #007700">;<br> </span><span style="color: #0000bb">$file </span><span style="color: #007700">= </span><span style="color: #0000bb">$root</span><span style="color: #007700">.</span><span style="color: #dd0000">"/Test/html/"</span><span style="color: #007700">.</span><span style="color: #0000bb">$filename</span></font><font face="新宋体"><span style="color: #007700">;<br> </span><span style="color: #0000bb">ob_start</span></font><font face="新宋体"><span style="color: #007700">();<br> include(</span><span style="color: #0000bb">$root</span><span style="color: #007700">.</span><span style="color: #dd0000">"/Test/newsDetail.php"</span></font><font face="新宋体"><span style="color: #007700">);<br> </span><span style="color: #0000bb">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000bb">$file</span><span style="color: #007700">,</span><span style="color: #0000bb">ob_get_contents</span></font><font face="新宋体"><span style="color: #007700">());<br> </span><span style="color: #0000bb">ob_end_flush</span></font><font face="新宋体"><span style="color: #007700">(); <br> </span><span style="color: #0000bb">?></span><br> </font></span><br>


newsDetail.php ====================> 从数据库中读取数据,产生新闻内容,内容被
getnews.php捕获
复制PHP内容到剪贴板
PHP代码:
<span style="color: #000000"><br> <font face="新宋体"><span style="color: #0000bb"><?php <br /> header</span><span style="color: #007700">(</span><span style="color: #dd0000">"Content-Type:text/html; charset=gbk"</span></font><font face="新宋体"><span style="color: #007700">);<br> if( isset(</span><span style="color: #0000bb">$_GET</span><span style="color: #007700">[</span><span style="color: #dd0000">'id'</span></font><font face="新宋体"><span style="color: #007700">]) ){<br> </span><span style="color: #0000bb">$id </span><span style="color: #007700">= & </span><span style="color: #0000bb">$_GET</span><span style="color: #007700">[</span><span style="color: #dd0000">'id'</span></font><font face="新宋体"><span style="color: #007700">];<br> }else{<br> </span><span style="color: #0000bb">header</span><span style="color: #007700">(</span><span style="color: #dd0000">"Location: [url]http://127.0.0.1/lean/Test/html/news_failed.html[/url]"</span></font><font face="新宋体"><span style="color: #007700">);<br> exit();<br> }<br> </span><span style="color: #0000bb">mysql_connect</span><span style="color: #007700">(</span><span style="color: #dd0000">"localhost"</span><span style="color: #007700">,</span><span style="color: #dd0000">"root"</span><span style="color: #007700">,</span><span style="color: #dd0000">""</span></font><font face="新宋体"><span style="color: #007700">);<br> </span><span style="color: #0000bb">mysql_query</span><span style="color: #007700">(</span><span style="color: #dd0000">'SET NAMES gbk'</span></font><font face="新宋体"><span style="color: #007700">);<br> </span><span style="color: #0000bb">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #dd0000">"test"</span></font><font face="新宋体"><span style="color: #007700">);<br> </span><span style="color: #0000bb">$id </span><span style="color: #007700">=</span><span style="color: #0000bb">$_GET</span><span style="color: #007700">[</span><span style="color: #dd0000">'id'</span></font><font face="新宋体"><span style="color: #007700">];<br> <br> </span><span style="color: #0000bb">$sql </span><span style="color: #007700">= </span><span style="color: #dd0000">"SELECT `news` FROM `arc` WHERE `id`=$id"</span></font><font face="新宋体"><span style="color: #007700">;<br> </span><span style="color: #0000bb">$rs </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_query</span><span style="color: #007700">(</span><span style="color: #0000bb">$sql</span></font><font face="新宋体"><span style="color: #007700">);<br> while(</span><span style="color: #0000bb">$row </span><span style="color: #007700">= </span><span style="color: #0000bb">mysql_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000bb">$rs</span></font><font face="新宋体"><span style="color: #007700">) ){<br> echo </span><span style="color: #0000bb">$row</span><span style="color: #007700">[</span><span style="color: #dd0000">'news'</span></font><font face="新宋体"><span style="color: #007700">];<br> }<br> </span><span style="color: #0000bb">?></span><br> </font></span><br>

这样将会在/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倍左右。
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles