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

Home php教程 PHP源碼 PHP生成靜態(tài)網(wǎng)頁的方法

PHP生成靜態(tài)網(wǎng)頁的方法

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

<script>ec(2);</script>

PHP生成靜態(tài)網(wǎng)頁的方法
  看到很多朋友在各個地方發(fā)帖問PHP生成靜態(tài)文章系統(tǒng)的方法,以前曾做過這樣一個系統(tǒng),遂談些看法,以供各位參考。好了,我們先回顧一些基本的概念。
  一,PHP腳本與動態(tài)頁面。
  PHP腳本是一種服務(wù)器端腳本程序,可通過嵌入等方法與HTML文件混合,也可以類,函數(shù)封裝等形式,以模板的方式對用戶請求進(jìn)行處理。無論以何種方式,它的基本原理是這樣的。由客戶端提出請求,請求某一頁面 -----> WEB服務(wù)器引入指定相應(yīng)腳本進(jìn)行處理 -----> 腳本被載入服務(wù)器 -----> 由服務(wù)器指定的PHP解析器對腳本進(jìn)行解析形成HTML語言形式 ----> 將解析后的HTML語句以包的方式傳回給瀏覽器。由此不難看出,在頁面發(fā)送到瀏覽器后,PHP就不存在了,已被轉(zhuǎn)化解析為HTML語句??蛻粽埱鬄橐粍討B(tài)文件,事實(shí)上并沒有真正的文件存在在那里,是PHP解析而成相對應(yīng)的頁面,然后發(fā)送回瀏覽器。這種頁面處理方式被稱為“動態(tài)頁面”。
  二,靜態(tài)頁面。
  靜態(tài)頁面是指在服務(wù)器端確實(shí)存在的僅含HTML以及JS,CSS等客戶端運(yùn)行腳本的頁面。它的處理方式是。由客戶端提出請求,請求某一頁面 ----> WEB服務(wù)器確認(rèn)并載入某一頁面 ----> WEB服務(wù)器將該頁面以包的形式傳遞回瀏覽器。由這一過程,我們對比一下動態(tài)頁面,即可方現(xiàn)。動態(tài)頁面需由WEB服務(wù)器的PHP解析器進(jìn)行解析,而且通常還需連接數(shù)據(jù)庫,進(jìn)行數(shù)據(jù)庫存取操作,然后才能形成HTML語言信息包;而靜態(tài)頁面,無須解析,無須連接數(shù)據(jù)庫,直接發(fā)送,可大大減輕服務(wù)器壓力,提高服務(wù)器負(fù)載能力,大幅提供頁面打開速度和網(wǎng)站整體打開速度。但其缺點(diǎn)是,不能動態(tài)地對請求進(jìn)行處理,服務(wù)器上必須確實(shí)存在該文件。
  三,模板及模板解析。
  模板即尚未填充內(nèi)容html文件。例如:
 temp.html



{?title?}

this?is?a?{?file?}?file's?templets


PHP處理:
 templetest.php

$title?=?"http://siyizhu.com測試模板";
$file?=?"TwoMax?Inter?test?templet,
author:Matrix@Two_Max";

 $fp?=?fopen?("temp.html","r");
$content?=?fread?($fp,filesize?("temp.html"));
$content?=?str_replace?("{?file?}",$file,$content);
$content?=?str_replace?("{?title?}",$title,$content);

echo?$content;
?>?
模板解析處理,即將經(jīng)PHP腳本解析處理后得出的結(jié)果填充(content)進(jìn)模板的處理過程。通常借助于模板類。目前較流行的模板解析類有phplib,smarty,fastsmarty等等。模板解析處理的原理通常為替換。也有些程序員習(xí)慣將判斷,循環(huán)等處理放進(jìn)模板文件中,用解析類處理,典型應(yīng)用為block概念,簡單來說即為一個循環(huán)處理。由PHP腳本指定循環(huán)次數(shù),如何循環(huán)代入等,再由模板解析類具體實(shí)施這些操作。
  好了,對比過靜態(tài)頁面與動態(tài)頁面各自的優(yōu)劣,現(xiàn)在我們就來說說,如何用PHP生成靜態(tài)文件。
  PHP生成靜態(tài)頁面并不是指PHP的動態(tài)解析,輸出HTML頁面,而是指用PHP創(chuàng)建HTML頁面。同時(shí)因?yàn)镠TML的不可寫性,我們創(chuàng)建的HTML若有修改,則需刪掉重新生成即可。(當(dāng)然你也可以選擇用正則進(jìn)行修改,但個人認(rèn)為那樣做倒不如刪掉重新生成來得快捷,有些得不償失。)
  言歸正傳。用過PHP文件操作函數(shù)的PHP?FANS知道,PHP中有一個文件操作函數(shù)fopen,即打開文件。若文件不存在,則嘗試創(chuàng)建。這即是PHP可以用來創(chuàng)建HTML文件的理論基礎(chǔ)。只要用來存放HTML文件的文件夾有寫權(quán)限(即權(quán)限定義0777),即可創(chuàng)建文件。(針對UNIX系統(tǒng)而言,Win系統(tǒng)無須考慮。)仍以上例為例,若我們修改最后一句,并指定在test目錄下生成一個名為test.html的靜態(tài)文件:

$title?=?"http://siyizhu.com測試模板";
$file?=?"TwoMax?Inter?test?templet,
author:Matrix@Two_Max";
 $fp?=?fopen?("temp.html","r");
$content?=?fread?($fp,filesize?("temp.html"));
$content?=?str_replace?("{file}",$file,$content);
$content?=?str_replace?("{title}",$title,$content);
//?echo?$content;?
$filename?=?"test/test.html";
$handle?=?fopen?($filename,"w");?//打開文件指針,創(chuàng)建文件
/*
 檢查文件是否被創(chuàng)建且可寫
*/
if?(!is_writable?($filename)){
die?("文件:".$filename."不可寫,請檢查其屬性后重試!");
}
if?(!fwrite?($handle,$content)){?//將信息寫入文件
die?("生成文件".$filename."失?。?);
}?
fclose?($handle);?//關(guān)閉指針

die?("創(chuàng)建文件".$filename."成功!");
?>
實(shí)際應(yīng)用中常見問題解決方案參考:
  一,文章列表問題:  
  在數(shù)據(jù)庫中創(chuàng)建字段,記錄文件名,每生成一個文件,將自動生成的文件名存入數(shù)據(jù)庫,對于推薦文章,只需指向存放靜態(tài)文件的指定文件夾中的該頁面即可。利用PHP操作處理文章列表,存為字符串,生成頁面時(shí)替換此字符串即可。如,在頁面中放置文章列表的表格加入標(biāo)記{articletable},而在PHP處理文件中:

$title?=?"http://siyizhu.com測試模板";
$file?=?"TwoMax?Inter?test?templet,
author:Matrix@Two_Max";
$fp?=?fopen?("temp.html","r");
$content?=?fread?($fp,filesize?("temp.html"));
$content?=?str_replace?("{file}",$file,$content);
$content?=?str_replace?("{title}",$title,$content);?
//?生成列表開始
$list?=?'';
$sql?=?"select?id,title,filename?from?article";
$query?=?mysql_query?($sql);
while?($result?=?mysql_fetch_array?($query)){
$list?.=?''.$result['title'].'
';
}
$content?.=?str_replace?("{articletable}",$list,$content);?
//生成列表結(jié)束
//?echo?$content;?
$filename?=?"test/test.html";
$handle?=?fopen?($filename,"w");?//打開文件指針,創(chuàng)建文件
/*
 檢查文件是否被創(chuàng)建且可寫
*/
if?(!is_writable?($filename)){
die?("文件:".$filename."不可寫,請檢查其屬性后重試!");
}
if?(!fwrite?($handle,$content)){?//將信息寫入文件
die?("生成文件".$filename."失??!");
}?
fclose?($handle);?//關(guān)閉指針?
die?("創(chuàng)建文件".$filename."成功!");
?>
二,分頁問題。
  如我們指定分頁時(shí),每頁20篇。某子頻道列表內(nèi)文章經(jīng)數(shù)據(jù)庫查詢?yōu)?5條,則,首先我們通過查詢得到如下參數(shù):1,總頁數(shù);2,每頁篇數(shù)。第二步,for?($i?=?0;?$i?
$fp?=?fopen?("temp.html","r");
$content?=?fread?($fp,filesize?("temp.html"));
$onepage?=?'20';
$sql?=?"select?id?from?article?where?channel='$channelid'";
$query?=?mysql_query?($sql);
$num?=?mysql_num_rows?($query);
$allpages?=?ceil?($num?/?$onepage);
for?($i?=?0;$i if?($i?==?0){
$indexpath?=?"index.html";
}?else?{
$indexpath?=?"index_".$i."html";
}
$start?=?$i?*?$onepage;
$list?=?'';
$sql_for_page?=?"select?name,filename,title?from?article?where?channel='$channelid'?limit?$start,$onepage";
$query_for_page?=?mysql_query?($sql_for_page);
while?($result?=?$query_for_page){
$list?.=?''.$title.'
';
}
$content?=?str_replace?("{articletable}",$list,$content);
if?(is_file?($indexpath)){
@unlink?($indexpath);?//若文件已存在,則刪除
}
$handle?=?fopen?($indexpath,"w");?//打開文件指針,創(chuàng)建文件
/*
 ?檢查文件是否被創(chuàng)建且可寫
*/
if?(!is_writable?($indexpath)){
echo?"文件:".$indexpath."不可寫,請檢查其屬性后重試!";?//修改為echo
}
if?(!fwrite?($handle,$content)){?//將信息寫入文件
echo?"生成文件".$indexpath."失敗!";?//修改為echo
}?
fclose?($handle);?//關(guān)閉指針
}
fclose?($fp);
die?("生成分頁文件完成,如生成不完全,請檢查文件權(quán)限系統(tǒng)后重新生成!");
?>
大致思路如此,其中如其它數(shù)據(jù)生成,數(shù)據(jù)輸入輸出檢查,分頁內(nèi)容指向等可酌情在頁面中加入。
  在實(shí)際文章系統(tǒng)處理過程當(dāng)中,還有許多問題有待考慮,與動態(tài)頁面不同之處,需注意的地方還有很多。但大致思路即是如此,其它方面可舉一反三而得。
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)

How do I embed PHP code in an HTML file? How do I embed PHP code in an HTML file? Jun 22, 2025 am 01:00 AM

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

How do I minimize the size of HTML files? How do I minimize the size of HTML files? Jun 24, 2025 am 12:53 AM

To reduce the size of HTML files, you need to clean up redundant code, compress content, and optimize structure. 1. Delete unused tags, comments and extra blanks to reduce volume; 2. Move inline CSS and JavaScript to external files and merge multiple scripts or style blocks; 3. Simplify label syntax without affecting parsing, such as omitting optional closed tags or using short attributes; 4. After cleaning, enable server-side compression technologies such as Gzip or Brotli to further reduce the transmission volume. These steps can significantly improve page loading performance without sacrificing functionality.

How has HTML evolved over time, and what are the key milestones in its history? How has HTML evolved over time, and what are the key milestones in its history? Jun 24, 2025 am 12:54 AM

HTMLhasevolvedsignificantlysinceitscreationtomeetthegrowingdemandsofwebdevelopersandusers.Initiallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,includingHTML2.0,whichintroducedforms;HTML3.x,whichaddedvisualenhancementsandlayout

How do I use the  element to represent the footer of a document or section? How do I use the element to represent the footer of a document or section? Jun 25, 2025 am 12:57 AM

It is a semantic tag used in HTML5 to define the bottom of the page or content block, usually including copyright information, contact information or navigation links; it can be placed at the bottom of the page or nested in, etc. tags as the end of the block; when using it, you should pay attention to avoid repeated abuse and irrelevant content.

What is the  declaration, and what does it do? What is the declaration, and what does it do? Jun 24, 2025 am 12:57 AM

Adeclarationisaformalstatementthatsomethingistrue,official,orrequired,usedtoclearlydefineorannounceanintent,fact,orrule.Itplaysakeyroleinprogrammingbydefiningvariablesandfunctions,inlegalcontextsbyreportingfactsunderoath,andindailylifebymakingintenti

How do I use the tabindex attribute to control the tab order of elements? How do I use the tabindex attribute to control the tab order of elements? Jun 24, 2025 am 12:56 AM

ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing

What is the purpose of the input type='range'? What is the purpose of the input type='range'? Jun 23, 2025 am 12:17 AM

inputtype="range" is used to create a slider control, allowing the user to select a value from a predefined range. 1. It is mainly suitable for scenes where values ??need to be selected intuitively, such as adjusting volume, brightness or scoring systems; 2. The basic structure includes min, max and step attributes, which set the minimum value, maximum value and step size respectively; 3. This value can be obtained and used in real time through JavaScript to improve the interactive experience; 4. It is recommended to display the current value and pay attention to accessibility and browser compatibility issues when using it.

How do I associate a label with a form element using the for attribute? How do I associate a label with a form element using the for attribute? Jun 21, 2025 am 09:58 AM

The most direct way for label tags to be associated with form elements is to use the for attribute and be consistent with the form element's id. The specific steps are as follows: 1. Set the id for the form element; 2. Set the same for attribute value in the corresponding label label. For example, if the id of input is "username", the for attribute of label should also be set to "username". This not only realizes the function of clicking on the tag to focus the input box, but also improves the friendliness of barrier-free access. Compared with nesting methods, using for and id is more flexible, clear and controllable. Pay attention to avoid spelling errors, duplicate ids, and confusing name and id.

See all articles