First, PHP scripts and dynamic pages.
The PHP script is a server-side scripter that can be mixed with the HTML file by embedding or other methods, or the user requests can be processed in the form of a class, a function package. Regardless of the way, its basic principle is like this. Proposed by the client, request a page -----> web server to introduce the specified script for processing -----> script is loaded into the server ----> The PHP parser specified by the server pair scripts Perform parsing to form an HTML language form ----> Transfer the parsed HTML statements into the browser in a package. It is not difficult to see that after the page is sent to the browser, PHP does not exist, and has been converted to an HTML statement. The customer request is a dynamic file, in fact, there is no real file exists there, is a PHP resolution to a corresponding page, and then send back the browser. This page processing method is called "dynamic page".
Second, the static page.
The static page refers to a page that only includes only HTML and JS, CSS, and other clients that exist only in the server side. Its processing method is. Request by the client, request a page ----> web server confirmation and load a page ----> web server passes the page back to the browser in the form of a package. From this process, we compare the dynamic page, that is, it is available. The dynamic page needs to be parsed by the PHP parser of the web server, and the database is often required to perform the data inventory, and then the HTML language packet can be formed; and the static page, no resolution, no need to connect the database, it can be sent directly. Redightly reduce server pressure, improve server load capacity, and greatly provide page open speed and website overall open speed. However, its disadvantage is that the request must not be dynamically processed, and the file must be existed on the server.
Third, template and template analysis.
The template has not yet filled the content HTML file. E.g:
Temp.html
Code:
PHP processing:
Templetest.php
$ 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. = str_replace ("{title}", $ TITLE, $ Content);
Echo $ content;?>
Template parsing processing, that is, the resulting result of the result fill after the PHP script parsing process is processed. Usually by means of template classes. Currently popular template parsing classes include phplib, smarty, fastsmarty, and more. The principle of template parsing processing is usually replaced. Some programmers habits will judge, looped, etc., in the template file, use the parsing class, typically apply to the Block concept, simply as a loop process. The number of cycles is specified by the PHP script, how to loop into the form, and then the template resolution class is specifically implemented. Ok, contrast the static pages and dynamic pages, now let's talk about how to generate static files with PHP.
PHP generation static page does not refer to PHP dynamic resolution, output HTML pages, but refers to the HTML page with PHP. At the same time, because HTML is unpackable, if there is a modification, if there is a modification, you can delete the regeneration. (Of course, you can also choose to modify it with regular, but individuals think that it is better to delete and regenerate, and some will not pay.)
The words retired. PHP Fans with PHP file operation functions know that there is a file operation function fopen in PHP, that is, opens the file. If the file does not exist, try creating. This is the theoretical basis for PHP can be used to create an HTML file. As long as the folder with the HTML file is written (ie, the privilege definition 0777) can create a file. (For UNIX systems, Win systems do not need to be considered.) Still in the example, if we modify the last sentence, and specify a static file called Test.html in the Test directory:
$ TITLE = "Tutomo 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. = str_replace ("{title}", $ TITLE, $ Content);
// echo $ content; $ filename = "test.html"; $ handle = fopen ($ filename, "w"); // Open the file pointer, create a file / * Check if the file is created and writable * / IF ("is_writable) {Die (" file: ". $ filename." Do not be written, please check after its property! ");} if (! fwrite ($ handle, $ content)) {/ / Write the information to file DIE ("Generate File". $ Filename. "Failed!");} Fclose ($ Handle); // Turns the pointer Die ("Create File". $ Filename. "Success!");? >
FAQ in practical applications Solutions Reference:
First, the list of articles: Create a field in the database, record the file name, each generate a file, save the automatically generated file name into the database, for the recommended article, simply point to the specified folder stored in the specified folder in the static file, ie can. Use the PHP operation to process the article list, save the string, and replace this string when generating a page. For example, place the table list in the page to add tag {articletable}, and in the PHP processing file: Code: $ title = "Timai 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. = STR_REPLACE (" {Title} ", $ TITLE, $ Content); // Generate a list Start $ list = ''; $ SQL =" SELECT ID, TILENAME from Article "; $ query = Mysql_Query; while ($ rist = mysql_fetch_array) {$ list. = ''. $ result ['title']. '';} $ content. = STR_REPLACE ("{articles", $ List, $ content); // Generate a list End // Echo $ content; $ filename = "test / test.html"; $ handle = fopen ($ filename, "w"); // Open file pointer, create files / * Check that the file is created and writable * / if (! ") {Die (" file: ". $ Filename." Do not be written, please check after its properties! ");} If (! FWRITE ($ Handle, $ Content) {// Write the information to file DIE ("Generate File". $ filename. "Failure!");} fclose ($ hand); // Close the pointer DIE ("Created File" $ filename. "Success!");?>
Second, paging problems.
20 pieces per page as we specify pagination. The article inside a sub-channel is 45, then, first we get the following parameters by query: 1, the total number of pages; 2, the number of each page. Step 2, for ($ I = 0; $ I 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 ("{articles}", $ list, $ content); IF (IS_FILE ($ indexpath) {@unlink ($ indexpath); // If the file already exists, delete} $ Handle = FOPEN ($ INDEXPATH, "W"); // Open the file pointer, create a file / * Check if the file is created and writable * / if (! is_writable ($ indexpath)) {echo "file:". Indexpath. "Do not be written, please check after its properties!"; // Modified to echo} if (! fwrite ($ handle, $ content) {// Write information to file Echo "Generate file". $ indexpath "Failure!"; // Modified to echo} fclose ($ hand); // Close pointer} fclose ($ fp); DIE ("Generate paging files complete, if generated is incomplete, please check the file permission system to regenerate ! ");" ?> Thoughts, such as other data generation, data input and output check, paging content points, etc., may be added to the page as appropriate. In the actual article system processing process, there are many problems to be considered, and there are many places where the dynamic page is required. But roughly thinking is, other aspects can be given to two.