国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
PHP代碼:
Home php教程 PHP源碼 PHP總結(jié)我的簡單靜態(tài)頁生成 過程,

PHP總結(jié)我的簡單靜態(tài)頁生成 過程,

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

<script>ec(2);</script>
一直用smarty的cache,但感覺還是要自己做一個,才有感覺。網(wǎng)上有很多牛人的功能比較完備,打算先自己搞簡單的再慢慢豐滿。這兩天做了一個比較簡單的,在hi.baidu.net/alex_wang58記錄一下。

一、用到的相關(guān)技術(shù)關(guān)鍵詞:PHP, Apache,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???mod_rewrite (RewriteCond,RewriteRule)地址重寫,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???ob系列函數(shù)緩沖
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???file_put_contents生成html

、流程:用戶發(fā)出請求url?id=x ,判斷文章是否存在
? ?? ?? ?? ?? ?? ?? ?? ?(1)存在則直接轉(zhuǎn)到對應(yīng)的Html頁面。
? ?? ?? ?? ?? ?? ?? ?? ?(2)不存在通過php讀取數(shù)據(jù)庫數(shù)據(jù),然后生成html文件,并存放到指定目錄。

三、實現(xiàn)方法
(1)地址重寫用Apahce的mod_rewrite模塊中的RewriteRule指令實現(xiàn)重寫(mod_rewrite的開啟和簡單規(guī)則見本博另一篇http://hi.baidu.com/alex%5Fwang5 ... 0346ffb3fb952e.html )。
(2)判斷文章是否存在用Apahce 的mod_rewrite模塊中的RewriteCond指令
(3)生成html文件:
? ?? ?? ???ob_star()打開緩沖,將讀取文章的php包含進來,然后用file_put_contents將獲得的緩沖內(nèi)容寫入指定HTMl文件。
四、代碼


/Test 目錄下的 .htaccess 文件內(nèi)容:

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 實現(xiàn)由第二句 RewriteRule ^index.html$ Test/news.php [L] 實現(xiàn)

news.php =============================> news.php將列出文章標題鏈接。
復(fù)制PHP內(nèi)容到剪貼板
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">//以防出現(xiàn)亂碼<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">//我的數(shù)據(jù)庫用的gbk編碼,請根據(jù)自己實際情況調(diào)整<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靜態(tài)頁實現(xiàn)
當點擊鏈接發(fā)出對 http://localhost/Test/html/news_3.html 的請求時
Apache將會判斷 news_3.html??是否存在,由 .htaccess中的第三句
RewriteCond %{REQUEST_FILENAME}??!-s
實現(xiàn):


? ???RewriteCond??是“定向
重寫發(fā)生條件”。REQUEST_FILENAME 這個參數(shù)是“客戶端請求的文件名”
'-s'??(是一個非空的常規(guī)文件[size]) 測試指定文件是否存在而且是一個尺寸大于0的常規(guī)的文件.??!表示匹配條件的反轉(zhuǎn)。
所以 RewriteCond 這句就表示當請求鏈接不存在時 執(zhí)行下面的 RewriteRule 規(guī)則。

所以當請求的
news_3.html 不存在時會重寫地址讓 getnews.php?id=3 來處理(否則如果news_3.html 存在則直接就加載該html文件)。

getnews.php ===================>功能:判斷參數(shù)傳輸?shù)耐暾?,并調(diào)用相應(yīng)文件生成html文件。

復(fù)制PHP內(nèi)容到剪貼板
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 ====================> 從數(shù)據(jù)庫中讀取數(shù)據(jù),產(chǎn)生新聞內(nèi)容,內(nèi)容被
getnews.php捕獲
復(fù)制PHP內(nèi)容到剪貼板
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 目錄下產(chǎn)生以 news_文章ID.html 命名的html文件。

PS: 一開始在判斷是否存在相應(yīng)html頁面時采用的是 php 內(nèi)置的 file_exists() 判斷,而不用Apache的
RewriteCond,也即沒有 RewriteCond %{REQUEST_FILENAME}??!-s??此瓶尚?,但結(jié)果會產(chǎn)生“循環(huán)重定向”的問題。
? ?? ? 當
news_3.html 不存在時 我們需要用 getnews.php生成news_3.html ,生成完畢后需要轉(zhuǎn)向到 news_3.html ,于是又形成了一次請求mod_rewrite又啟動把 news_3.html重寫為 getnews.php?id=3 這就形成了死循環(huán)了。所以把文件存在性的判斷交給 RewriteCond ,指定的html文件不存在時才啟用重寫規(guī)則。這樣循環(huán)重定向的問題就沒有了。
? ?? ? 一開始沒有采用fopen打開
newsDetail.php,然后再將生成的內(nèi)容fwrite成html文件,然后include輸出靜態(tài)頁面。后來在fhjr999的提醒下,改為:將newDetail.php包含進getnews.php,通過ob系列函數(shù)將生成的內(nèi)容放入緩沖,然后再生成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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Applying Semantic Structure with article, section, and aside in HTML Applying Semantic Structure with article, section, and aside in HTML Jul 05, 2025 am 02:03 AM

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

How Do You Pass Variables by Value vs. by Reference in PHP? How Do You Pass Variables by Value vs. by Reference in PHP? Jul 08, 2025 am 02:42 AM

InPHP,variablesarepassedbyvaluebydefault,meaningfunctionsorassignmentsreceiveacopyofthedata,whilepassingbyreferenceallowsmodificationstoaffecttheoriginalvariable.1.Whenpassingbyvalue,changestothecopydonotimpacttheoriginal,asshownwhenassigning$b=$aorp

How to handle File Uploads securely in PHP? How to handle File Uploads securely in PHP? Jul 08, 2025 am 02:37 AM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

BONK ETF craze: The catalyst for the surge in meme coins? BONK ETF craze: The catalyst for the surge in meme coins? Jul 04, 2025 pm 09:06 PM

BONK soars! ETF application, technological breakthroughs and Solana's strength are key. Is this the beginning of the meme currency revolution, or is it another bubble? BONKETF craze: The catalyst for meme coin takeoff? BONK price has risen nearly 20% in the past 24 hours, driven by ETF expectations, technical bullishness and strong performance of Solana Network. Will BONKETF be the catalyst for the meme currency revolution? ETF application ignites market enthusiasm. The double-long application of BONKETF submitted by TuttleCapitalManagement has become an important driving force at present. Relevant documents show that the ETF may be officially launched as soon as July 16. This news has inspired investors to invest in structured BONK investment products

PHP find the position of the last occurrence of a substring PHP find the position of the last occurrence of a substring Jul 09, 2025 am 02:49 AM

The most direct way to find the last occurrence of a substring in PHP is to use the strrpos() function. 1. Use strrpos() function to directly obtain the index of the last occurrence of the substring in the main string. If it is not found, it returns false. The syntax is strrpos($haystack,$needle,$offset=0). 2. If you need to ignore case, you can use the strripos() function to implement case-insensitive search. 3. For multi-byte characters such as Chinese, the mb_strrpos() function in the mbstring extension should be used to ensure that the character position is returned instead of the byte position. 4. Note that strrpos() returns f

How to group options within a select dropdown using html? How to group options within a select dropdown using html? Jul 04, 2025 am 03:16 AM

Use tags in HTML to group options in the drop-down menu. The specific method is to wrap a group of elements and define the group name through the label attribute, such as: 1. Contains options such as apples, bananas, oranges, etc.; 2. Contains options such as carrots, broccoli, etc.; 3. Each is an independent group, and the options within the group are automatically indented. Notes include: ① No nesting is supported; ② The entire group can be disabled through the disabled attribute; ③ The style is restricted and needs to be beautified in combination with CSS or third-party libraries; plug-ins such as Select2 can be used to enhance functions.

Implementing Clickable Buttons Using the HTML button Element Implementing Clickable Buttons Using the HTML button Element Jul 07, 2025 am 02:31 AM

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

How to create hyperlinks using the anchor tag in html? How to create hyperlinks using the anchor tag in html? Jul 05, 2025 am 01:41 AM

The method of creating a hyperlink in HTML is as follows: 1. Use the tag, set the href attribute to specify the target address, and the tag content is clickable text; 2. When linking an internal page, href can use a relative or absolute path; 3. Add target="_blank" to achieve a new window opening, and it is recommended to cooperate with rel="noopener"; 4. Use #id to achieve jump within the page, and the target element needs to set the corresponding id. Mastering these structures and attributes can achieve diverse link functions.

See all articles