At present, the CMS system uses the page static, the advantage is: one is to reduce the burden of its server, and the second is because the HTML static page generated, so its website is search engines to find a greater probability, that is, to promote the site.
1. Static classification of pages
True static: The static page file generated by the program, when we visit, is the direct access to the static page.
Pseudo-Static: is a dynamic URL that looks like a static URL. In other words, dynamic Web pages are implemented to remove the parameters of a dynamic Web page by overriding the URL method, but there is no need to implement a rewritten page in the actual Web page directory. Refer to the previous section for details: http://blog.csdn.net/zhao1234567890123456/article/details/38569139
2, the implementation of true static method
[1] using the template to generate static files, through their own files or other frameworks, such as: Smarty can be used to achieve static website. Here is a simple program to understand the use of templates to generate static files.
Template file template.htm:
[HTML]View Plaincopyprint?
- < HTML >
- < Head >
- < title > %title% </ title >
- </ Head >
- < Body >
- < H1 > %title% </ H1 >
- < HR >
- < Pre > %body% </ Pre >
- </ Body >
- </ HTML >
PHP Files:[PHP]View Plaincopyprint?
- <?php
- The //replace function is used to replace the keywords in the content read from the template file with the contents of the variable
- function Replace ($row)
- {
- $title = "article title" ; <span style="font-family:arial, Helvetica, Sans-serif;" > //define variables to replace </span>
- $body = "Here is the subject of the article" ;
- $row = Str_replace ("%title%",$title, $row); <span style="font-family:arial, Helvetica, Sans-serif;" > //Replace keywords in the parameters </span>
- $row = Str_replace ("%body%",$body, $row);
- return $row; <span style="font-family:arial, Helvetica, Sans-serif;" > //Returns the result of the replacement </span>
- }
- $f _tem = fopen ( , ); <span style= "font-family:arial, Helvetica, Sans-serif;" ; //template file pointer </span>
- $f _new = fopen ("new.htm","W"); <span style="font-family:arial, Helvetica, Sans-serif;" > //generated file pointer </span>
- //Iterate through the template file, reading one line at a time
- while (! feof ($f _tem))
- {
- $row = fgets ($f _tem);
- $row = Replace ($row); //Replace keywords in read-in content
- Fwrite ($f _new, $row); //writes the replaced content to the generated HTML file
- }
- Fclose ($f _new); <span style="font-family:arial, Helvetica, Sans-serif;" > //Close file pointer </span>
- Fclose ($f _tem);
- ?>
The <?php//replace function is used to replace the keywords in the content read from the template file with the contents of the variable function replace ($row) {$title = "article title"; <span style= "font-family:arial, Helvetica, Sans-serif;" >//defines the variable used for substitution </span> $body = "Here is the subject of the article"; $row = Str_replace ("%title%", $title, $row); <span style= "font-family:arial, Helvetica, Sans-serif;" >//the keywords in the replacement parameter </span> $row = Str_replace ("%body%", $body, $row); return $row; <span style= "font-family:arial, Helvetica, Sans-serif;" >//returns the replacement result </span>} $f _tem = fopen ("template.htm", "R"); <span style= "font-family:arial, Helvetica, Sans-serif;" >//template file pointer </span> $f _new = fopen ("new.htm", "w"); <span style= "font-family:arial, Helvetica, Sans-serif;" The >//generated file pointer </span>//loops through the template file, reading one row at a time (!feof ($f _tem)) {$row = fgets ($f _tem); $row = Replace ($row); Replace the keyword fwrite in the read-in content ($f _new, $row); Writes the replaced content to the generated HTML file} fclose ($f _new); <span style= "font-family:arial, Helvetica, Sans-serif;" >//Close file pointer </span>fclose ($f _tem);?>
[2] Write a static file through a PHP program, here is a simple case
[PHP]View Plaincopyprint?
- <?php
- Ob_start ();
- ?>
- <title> This program is called testing </title>
- <body>
- This is a test.
- <body>
- <?php
- $out 1 = Ob_get_contents ();
- Ob_end_clean ();
- $fp = fopen ("ceshi.html","W");
- if (! $fp )
- {
- Echo file does not exist ";
- Exit ();
- }
- Else
- {
- Fwrite ($fp,$out 1);
- Fclose ($fp);
- Echo "Success";
- }
- ?>
PHP page static