Static php page

Source: Internet
Author: User
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?

  1. % Title %
  2. % Title %
  3. %body%

            %title%                %title%                
%body%
Php file:

[Php]View plain copy print?

  1. // Replace function is used to Replace the keyword in the content read from the template file with the content in the variable
  2. Function Replace ($ row)
  3. {
  4. $ Title = "article title"; // defines the variable to be replaced
  5. $ Body = "this is the subject of the article ";
  6. $ Row = str_replace ("% title %", $ title, $ row); // replace the keyword in the parameter
  7. $ Row = str_replace ("% body %", $ body, $ row );
  8. Return $ row; // return the replaced result
  9. }
  10. $ F_tem = fopen ("template.htm", "r"); // template file pointer
  11. $ F_new = fopen ("new.htm", "w"); // generated file pointer
  12. // Read the template file cyclically and read a row each time
  13. While (! Feof ($ f_tem ))
  14. {
  15. $ Row = fgets ($ f_tem );
  16. $ Row = Replace ($ row); // Replace the keyword in the read content
  17. Fwrite ($ f_new, $ row); // write the replaced content to the generated HTML file
  18. }
  19. Fclose ($ f_new); // close the file pointer
  20. Fclose ($ f_tem );
  21. ?>

 
[2] using a php program to write static files. a simple case is provided here.

[Php]View plain copy print?

  1. Ob_start ();
  2. ?>
  3. This program is called testing
  4. This is a test
  5. $ Out1 = ob_get_contents ();
  6. Ob_end_clean ();
  7. $ Fp = fopen ("ceshi.html", "w ");
  8. If (! $ Fp)
  9. {
  10. Echo file does not exist ";
  11. Exit ();
  12. }
  13. Else
  14. {
  15. Fwrite ($ fp, $ out1 );
  16. Fclose ($ fp );
  17. Echo "Success ";
  18. }
  19. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.