PHP總結我的簡單靜態(tài)頁生成 過程,
Jun 08, 2016 pm 05:33 PM一、用到的相關技術關鍵詞:PHP, Apache,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???mod_rewrite (RewriteCond,RewriteRule)地址重寫,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???ob系列函數(shù)緩沖
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???file_put_contents生成html
二、流程:用戶發(fā)出請求url?id=x ,判斷文章是否存在
? ?? ?? ?? ?? ?? ?? ?? ?(1)存在則直接轉到對應的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將獲得的緩沖內容寫入指定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 實現(xiàn)由第二句 RewriteRule ^index.html$ Test/news.php [L] 實現(xiàn)
news.php =============================> news.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">//以防出現(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ù)自己實際情況調整<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ī)的文件.??!表示匹配條件的反轉。
所以 RewriteCond 這句就表示當請求鏈接不存在時 執(zhí)行下面的 RewriteRule 規(guī)則。
所以當請求的news_3.html 不存在時會重寫地址讓 getnews.php?id=3 來處理(否則如果news_3.html 存在則直接就加載該html文件)。
getnews.php ===================>功能:判斷參數(shù)傳輸?shù)耐暾?,并調用相應文件生成html文件。
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)生新聞內容,內容被getnews.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 目錄下產(chǎn)生以 news_文章ID.html 命名的html文件。
PS: 一開始在判斷是否存在相應html頁面時采用的是 php 內置的 file_exists() 判斷,而不用Apache的 RewriteCond,也即沒有 RewriteCond %{REQUEST_FILENAME}??!-s。看似可行,但結果會產(chǎn)生“循環(huán)重定向”的問題。
? ?? ? 當news_3.html 不存在時 我們需要用 getnews.php生成news_3.html ,生成完畢后需要轉向到 news_3.html ,于是又形成了一次請求mod_rewrite又啟動把 news_3.html重寫為 getnews.php?id=3 這就形成了死循環(huán)了。所以把文件存在性的判斷交給 RewriteCond ,指定的html文件不存在時才啟用重寫規(guī)則。這樣循環(huán)重定向的問題就沒有了。
? ?? ? 一開始沒有采用fopen打開newsDetail.php,然后再將生成的內容fwrite成html文件,然后include輸出靜態(tài)頁面。后來在fhjr999的提醒下,改為:將newDetail.php包含進getnews.php,通過ob系列函數(shù)將生成的內容放入緩沖,然后再生成html文件。ob的效率是前者的20倍左右。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











HTML?? ??? ??? ????? ???? ??? ?? ???, ??? ? SEO ??? ?? ? ? ????. 1. ??? ????? ??? ?? ??? ? ??? ??? ????? ?? ????????. 2. ????? ??? ???? ?? ?? ???? ???? ???? ?? ??? ?????. 3. ? ???? ??? ?? ??? ????? ??? ? ?? ?? ?? ?? ????? ?? ??? ????. ?? ???? ??? ??????? ?? ??? ??? ??? ???? ???? ??? ??? ?? ??? ???? ???????.

PHP ?? ???? ???? ????? ?? ? ??? ???? ?? ?? ? ??? ???? ?? ??? ?????? ??? ??? ? ? ???????. 1. ??? ?? CSRF? ???? ?? ??? ??? ???? ?????? ??? ???? FINFO_FILE? ?? ?? MIME ??? ?????. 2. ??? ??? ??? ???? ??? ?? ??? ?? ? WEB ????? ??? ???? ??????. 3. PHP ?? ??? ?? ? ?? ???? NGINX/APACHE? ??? ????? ?? ???? ?????. 4. GD ?????? ??? ? ?? ???? ??? ?? ??? ?? ????.

Bonk Soars! ETF ??, ?? ?? ? Solana? ??? ?????. ??? ? ?? ??? ?????, ??? ? ?? ?????? Bonketf ?? : ? ?? ??? ??? Bonk Price? ?? 24 ?? ?? ETF ??, ??? ?? ? Solana ????? ??? ??? ?? ?? 20% ??????. Bonketf? MEME ?? ??? ???? ???? ETF ?? ????? ??? ??? ?? ?? ???. TuttleCapitalManagement? ?? ? Bonketf? ?? ? ??? ?? ??? ?????????. ?? ??? ??? ETF? 7 ? 16 ?? ????? ?? ? ? ????.? ??? ????? ??? ? Bonk Investment Products? ????? ????????.

PHP?? ????? ??? ??? ?? ?? ???? ??? strrpos () ??? ???? ????. 1. strrpos () ??? ???? ?? ????? ?? ???? ??? ?? ???? ?? ????. ???? ??? ??? ?????. ??? strrpos??? ($ haystack, $ ??, $ offset = 0). 2. ??? ?? ???? ?? strripos () ??? ???? ??? ???? ?? ??? ??? ? ????. 3. ???? ?? ?? ??? ??? ?? MBString ??? MB_Strrpos () ??? ???? ??? ?? ?? ?? ??? ??????????. 4. strrpos ()? f? ????? ?? ??????

?? ?? ???? HTML?? ??? ?? ???? ??????. ???? ??? ?? ??? ???? ??? ?? ??? ??? ?? ?? ??? ???? ????. 1. ??, ???, ??? ?? ?? ??? ???? ????. 2. ??, ???? ?? ?? ??? ???? ????. 3. ?? ?? ???? ?? ? ??? ???? ?? ?????. ?? ??? ??? ?????. ? ?? ??? ??? ??? ?? ???? ? ? ????. style ???? ???? ??? CSS ?? ?? ?????? ?? ???????. Select2? ?? ????? ??? ????? ? ??? ? ????.

AJAX ??? ?? ( '?? : ...')? ???? ?? ??? ????? ???? ??? ????? ???? ?? ?????. AJAX ???? ??? ?? ? 302 ?? ?? ? ?? ?? ??? ?? ??? ????? ?? ?? ???? ?????. ???? ??? ????. 1. PHP?? JSON ???? ???? ?? URL? ?????. 2. ??? ?? Ajax ???? ???? ??? ???? Window.location.href? ???? ?????. 3. PHP ??? ?? ?? ??? ??? ?? JSON? ???? ??????. 4. ??? ??? ??? ????? ??? CORS ??? ???????. 5. ?? ??? ????? ?? ???? ????? ??? ??? ? ????.

HTML ?? ??? ???? ?? ??? ??? ???? ?? ?? ?? ? ???? ?? ??? ????????. 1. ????? ??? ??? ????? ?? ? ?? ?? (? : ??, ??, ???)? ?? ??? ?????. 2. JavaScript? ?? ?? ? ??? ???? ID? ?? ??? ?? ??? ??? ???? ?? ? ????. 3. CSS? ???? ???, ???, ?? ??? ? ??/?? ?? ??? ???? ???? ??? ???? ??? ??? ??????. 4. ???? ????????? : ???? ? ??? ????? ??? ???? JS ???? ???? ????? ???? ??? ???? ??? ??? ??? ???? ??? ?????. ??? ???????
