PHP page static

Source: Internet
Author: User
Tags vars

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?
  1. < HTML >   
  2. < Head >   
  3. < title > %title% </ title >   
  4. </ Head >   
  5. < Body >   
  6. < H1 > %title% </ H1 >   
  7. < HR >   
  8. < Pre > %body% </ Pre >   
  9. </ Body >   
  10. </ HTML >   
PHP Files:

[PHP]View Plaincopyprint?
  1. <?php
  2. The //replace function is used to replace the keywords in the content read from the template file with the contents of the variable  
  3. function Replace ($row)
  4. {
  5. $title = "article title"                            ; <span style="font-family:arial, Helvetica, Sans-serif;" > //define variables to replace </span>  
  6. $body = "Here is the subject of the article"       ;
  7. $row = Str_replace    ("%title%",$title, $row); <span style="font-family:arial, Helvetica, Sans-serif;" > //Replace keywords in the parameters </span>  
  8. $row = Str_replace       ("%body%",$body, $row);
  9. return                                    $row; <span style="font-family:arial, Helvetica, Sans-serif;" > //Returns the result of the replacement </span>  
  10. }
  11. $f _tem = fopen ( , );                  <span style= "font-family:arial, Helvetica, Sans-serif;" ; //template file pointer </span>  
  12. $f _new = fopen                      ("new.htm","W"); <span style="font-family:arial, Helvetica, Sans-serif;" > //generated file pointer </span>  
  13. //Iterate through the template file, reading one line at a time  
  14. while (! feof ($f _tem))
  15. {
  16. $row = fgets  ($f _tem);
  17. $row                        = Replace ($row); //Replace keywords in read-in content
  18. Fwrite ($f _new, $row); //writes the replaced content to the generated HTML file
  19. }
  20. Fclose ($f _new); <span style="font-family:arial, Helvetica, Sans-serif;" > //Close file pointer </span>  
  21. Fclose ($f _tem);
  22. ?>
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?
  1. <?php
  2. Ob_start ();
  3. ?>
  4. <title> This program is called testing </title>
  5. <body>
  6. This is a test.
  7. <body>
  8. <?php
  9. $out 1   = Ob_get_contents ();
  10. Ob_end_clean ();
  11. $fp = fopen   ("ceshi.html","W");
  12. if (! $fp )   
  13. {
  14. Echo    file does not exist ";
  15. Exit    ();
  16. }
  17. Else   
  18. {
  19. Fwrite ($fp,$out 1);
  20. Fclose ($fp);
  21. Echo    "Success";
  22. }
  23. ?>

PHP page static

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.