Php static pages currently, the CMS system mostly uses static pages. the advantage of doing so is: first, it can reduce the burden on its server, and second, it is because HTML static pages are generated, therefore, their websites are more likely to be searched by search engines, that is, for website promotion.
1. static page classification
True static: a static page file is generated through a program. when we access the static page, it is directly accessed.
Pseudo static: a dynamic website looks like a static website. In other words, dynamic web pages remove the parameters of dynamic web pages by rewriting the URL method, but there is no need to rewrite the pages in the actual web page Directory. Specific can refer to the previous chapter: http://blog.csdn.net/zhao1234567890123456/article/details/38569139
2. Real and static implementation method
[1] using templates to generate static files, you can use your own files or other frameworks. for example, Smarty can achieve static website. Here we provide a simple program to understand the use of templates to generate static files.
Template.htm:
[Html]View plain copy print?
- % Title %
- % Title %
-
%body%
%title% %title% %body%
Php file:
[Php]View plain copy print?
- // Replace function is used to Replace the keyword in the content read from the template file with the content in the variable
- Function Replace ($ row)
- {
- $ Title = "article title"; // defines the variable to be replaced
- $ Body = "this is the subject of the article ";
- $ Row = str_replace ("% title %", $ title, $ row); // replace the keyword in the parameter
- $ Row = str_replace ("% body %", $ body, $ row );
- Return $ row; // return the replaced result
- }
- $ F_tem = fopen ("template.htm", "r"); // template file pointer
- $ F_new = fopen ("new.htm", "w"); // generated file pointer
- // Read the template file cyclically and read a row each time
- While (! Feof ($ f_tem ))
- {
- $ Row = fgets ($ f_tem );
- $ Row = Replace ($ row); // Replace the keyword in the read content
- Fwrite ($ f_new, $ row); // write the replaced content to the generated HTML file
- }
- Fclose ($ f_new); // close the file pointer
- Fclose ($ f_tem );
- ?>
[2] using a php program to write static files. a simple case is provided here.
[Php]View plain copy print?
- Ob_start ();
- ?>
- This program is called testing
- This is a test
- $ Out1 = ob_get_contents ();
- Ob_end_clean ();
- $ Fp = fopen ("ceshi.html", "w ");
- If (! $ Fp)
- {
- Echo file does not exist ";
- Exit ();
- }
- Else
- {
- Fwrite ($ fp, $ out1 );
- Fclose ($ fp );
- Echo "Success ";
- }
- ?>