PHP static page generation details, read PHP static page generation details, copyright statement: can be reproduced at will, please be sure to mark the original source and author information of the article in the form of a hyperlink when reprinting and this statement from: http://www.otm.cn author: Matrix @ Two_Max let's review some basic concepts first. I. PHP scripts and dynamic pages. The PHP script is a server "> <LINKhref =" h
Copyright disclaimer: you can reprint the document at will. during reprinting, you must mark the original source and author information of the article as hyperlinks and this statement.
This article from: http://www.otm.cn author: Matrix @ Two_Max
Let's first review some basic concepts.
I. PHP scripts and dynamic pages.
A PHP script is a server-side scripting program that can be mixed with HTML files by embedding methods, classes, function encapsulation, and other forms to process user requests in a template. Regardless of the method, the basic principle is as follows. Requests made by the client, request a page -----> The WEB server introduces the specified script for processing -----> The script is loaded to the server -----> the PHP parser specified by the server parses the script to form an HTML language ----> the parsed HTML statement is returned to the browser as a package. It is not hard to see that after the page is sent to the browser, PHP does not exist and has been converted into HTML statements. The customer requests a dynamic file, in fact there is no real file, it is a PHP parsing to form a corresponding page, and then sent back to the browser. This page processing method is called "dynamic page ".
2. static pages.
A static page is a page on the server that contains only HTML, JS, CSS, and other scripts on the client. The processing method is. The client sends a request to a page ----> The WEB server confirms and loads a page ----> The WEB server sends the page back to the browser as a package. From this process, we can compare the dynamic page to see it now. Dynamic pages must be parsed by the PHP parser of the WEB server. Generally, you need to connect to the database and perform database access operations to form an HTML language information package. static pages do not need to be parsed, direct sending without connecting to the database can greatly reduce the pressure on the server, improve the server load capability, and greatly provide the page opening speed and the overall website opening speed. However, the disadvantage is that the request cannot be processed dynamically and the file must exist on the server.
3. parsing templates and templates.
The template does not fill in the html file. For example:
Temp.html
Code:
{Title}
This is a {file} file's templets
PHP processing:
Templetest. php
Code:
$ Title = "PHP enthusiast test template ";
$ File = "TwoMax Inter test templet,
Author: Sheyi ";
$ 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;
?>
Template parsing process: the result filling (content) obtained after parsing and processing by the PHP script is entered into the processing process of the template. The template class is usually used. Currently, popular template parsing classes include phplib, smarty, fastsmarty, and so on. The principle of template parsing is usually replaced. Some programmers are also used to putting judgment, loop, and other processing into the template file, using parsing class processing. A typical application is the block concept, which is simply a loop processing. The PHP script specifies the number of loops, how to cyclically substitute, and so on, and then the template parsing class specifically implements these operations.
Well, we have compared the advantages and disadvantages of static pages and dynamic pages. now let's talk about how to use PHP to generate static files.
Generating static pages in PHP does not refer to the dynamic parsing of PHP, but to outputting HTML pages. Instead, PHP is used to create HTML pages. At the same time, because of the non-writability of HTML, if the HTML we create is modified, we need to delete it and generate it again. (You can also modify the regular expression, but I personally think that it is better to delete and regenerate the regular expression, which is not worth the candle .)
Let's get down to the truth. Php fans who have used PHP file operation functions know that there is a file operation function fopen in PHP, that is, open the file. If the file does not exist, try to create it. This is the theoretical basis for PHP to create HTML files. As long as the folder used to store HTML files has the write permission (that is, permission definition 0777), you can create the file. (For UNIX systems, Windows systems do not need to be considered .) Take the previous example as an example. if we modify the last modification, we must generate a static file named test.html under the test Directory:
Code:
$ Title = "Tomai international test template ";
$ 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"); // open the file pointer and create a file
/*
Check whether the file is created and writable.
*/
If (! Is_writable ($ filename )){
Die ("File:". $ filename. "cannot be written. check its attributes and try again! ");
}
If (! Fwrite ($ handle, $ content) {// write information to the file
Die ("generate file". $ filename. "failed! ");
}
Fclose ($ handle); // Close the pointer
Die ("Creating File". $ filename. "successful! ");
?>
For solutions to common problems in practical applications, refer:
I. list of articles:
Create a field in the database and record the file name. each time a file is generated, the automatically generated file name is saved to the database. for recommended articles, you only need to point to the page in the specified folder where the static files are stored. Use the PHP operation to process the article list and save it as a string. replace this string when generating the page. For example, add the Mark {articletable} to the table where the article list is placed on the page, and process the file in PHP:
Code:
$ Title = "Tomai international test template ";
$ 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 );
// Starts generating the list
$ 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 );
// The Generation list ends.
// Echo $ content;
$ Filename = "test/test.html ";
$ Handle = fopen ($ filename, "w"); // open the file pointer and create a file
/*
Check whether the file is created and writable.
*/
If (! Is_writable ($ filename )){
Die ("File:". $ filename. "cannot be written. check its attributes and try again! ");
}
If (! Fwrite ($ handle, $ content) {// write information to the file
Die ("generate file". $ filename. "failed! ");
}
Fclose ($ handle); // Close the pointer
Die ("Creating File". $ filename. "successful! ");
?>
II. paging problems.
For example, we specify 20 pages per page. If the number of articles in a subchannel list is 45 in the database, we first obtain the following parameters through the query: 1, the total number of pages; 2, the number of entries per page. Step 2: for ($ I = 0; $ I <allpages; $ I ++), page element acquisition, analysis, and article generation are all executed in this loop. The difference is that die ("create file". $ filename. "succeeded! "; This sentence is removed and displayed after the loop, because the statement will stop the program execution. Example:
Code:
$ 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 <$ allpages; $ 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); // delete a file if it already exists
}
$ Handle = fopen ($ indexpath, "w"); // open the file pointer and create a file
/*
Check whether the file is created and writable.
*/
If (! Is_writable ($ indexpath )){
Echo "File:". $ indexpath. "cannot be written. check its attributes and try again! "; // Modify it to echo
}
If (! Fwrite ($ handle, $ content) {// write information to the file
Echo "generating file". $ indexpath. "failed! "; // Modify it to echo
}
Fclose ($ handle); // Close the pointer
}
Fclose ($ fp );
Die ("Generation of paging files is complete. if the generation is incomplete, check the file permission system and generate a new one! ");
?>
The general idea is that, for example, other data generation, data input and output check, and paging content pointing can be added to the page as appropriate.
In the actual article system processing process, there are still many issues to consider. different from dynamic pages, there are still many things to note. However, the general idea is that, in other aspects, the opposite is true.