PHP static page (reprint)

Source: Internet
Author: User
Tags php template

What is PHP static

PHP static simple understanding is to make the site Generation page in the form of static HTML in front of the visitors, PHP static pure static and pseudo-static, the difference is that PHP generated static page processing mechanism is different.

The PHP execution page is pre-converted to HTML and is one of the so-called static PHP methods.



Why make Web pages static

First, speed up the page to open the browsing speed, static pages do not need to connect the database open faster than the dynamic page has significantly improved;
Second, conducive to search engine optimization Seo,baidu, Google will first include static pages, not only is included in the fast also included in the whole;
Third, reduce the burden on the server, browse the Web without calling the system database;
Four, the website is more secure, HTML page will not be affected by PHP related vulnerability; Looking at a larger site is basically a static page, and can reduce attacks, anti-SQL injection.

The effect of static:

Improve the response speed of the website, reduce the load of the server;
SEO for the search engine;
The use of PHP output buffer directly written HTML, timed background time to scan the site, is relatively easy to achieve the static page.


When a database error occurs, it does not affect normal site access.
Generate HTML article Although the operation of the trouble, the program is complicated, but in order to better search, for faster, more secure, these sacrifices are worth.

How PHP generates static HTML pages

Generate static pages with PHP templates

PHP template Implementation of the static is very convenient, such as the installation and use of PHP smarty to achieve static site, you can also write a set of template parsing rules, common can imitate various types of CMS template rules.

1. Generate a static page using the PHP file read-write function and the OB cache mechanism
For example, the dynamic Details page address for a product is: http://xxx.com?goods.php?gid=112
So here we read the contents of this detail page from this address once, and then save as static page, the next time someone accesses this product Details page dynamic address, we can
Output the corresponding static content file that has been generated directly.

1 $gid=$_get[' GID ']+0;//Product ID2 $goods _statis_file= "Goods_file_".$gid.". HTML ";//corresponding static page file3 $expr= 3600*24*10;//static file validity, 10 days4 if(file_exists($goods _statis_file)){5   $file _ctime=Filectime($goods _statis_file);//File creation Time6      if($file _ctime+$expr- Time()){//If not expired7       Echo file_get_contents($goods _statis_file);//output static file contents8          Exit;9}Else{//if it has expiredTen          unlink($goods _statis_file);//delete an outdated static page file One          Ob_start(); A    -             //read data from a database and assign values to related variables -    the //include ("xxx.html");//load the corresponding Product Details page template -    -             $content=ob_get_contents();//Assign the details page content to the $content variable -             file_put_contents($goods _statis_file,$content);//write content to the corresponding static file +             Ob_end_flush();//Export Product Details page information -      } +}Else{ A  Ob_start(); at    -  //read data from a database and assign values to related variables -    - //include ("xxx.html");//load the corresponding Product Details page template -    -  $content=ob_get_contents();//Assign the details page content to the $content variable in  file_put_contents($goods _statis_file,$content);//write content to the corresponding static file -  Ob_end_flush();//Export Product Details page information to    +}

Before Static:

<?phpfor ($i =0; $i <1000; $i + +) {echo "bull<br>";}? >

Static:

<?phpob_start (); for ($i =0; $i <1000; $i + +) {echo "bull<br>";} $str = Ob_get_contents (); Ob_end_clean () $fp = fopen ("test.html", "W"), Fwrite ($fp, $STR); fclose ($FP); Echo <div> Success Write</div> ";? >

  

The code will be pre-formed a test.html, white is actually used space in exchange for time, such as the news site, the content of the release basically finished is not changed, more suitable for static into HTML.

Direct to this HTML page when the user accesses it.

Introduction to additional related functions


1), flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.

2), Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.

3), Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.

4), Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.

5), Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any).

6), Ob_end_clean: Delete the contents of the internal buffer, and close the internal buffer
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!

7), Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()。

2. Use NoSQL to read content from memory (in fact, this is not static but caching);

Take Memcache as an example:

1 $gid=$_get[' GID ']+0;//Product ID2 $goods _statis_content= "Goods_content_".$gid;//corresponding Key3 $expr= 3600*24*10;//valid for 10 days4   5 $mem=NewMemcache;6 $mem--->connect (' memcache_host ', 11211);7   8 $mem _goods_content=$mem->get ($goods _statis_content);9   Ten    One    A if($mem _goods_content){ -   Echo $mem _goods_content; -}Else{ the  Ob_start(); -    -  //read data from a database and assign values to related variables -    + //include ("xxx.html");//load the corresponding Product Details page template -    +  $content=ob_get_contents();//Assign the details page content to the $content variable A  $mem->add ($goods _statis_content,$content,false,$expr); at  Ob_end_flush();//Export Product Details page information -    -}

Memcached is the key value one by one corresponds, the key default maximum cannot exceed 128 bytes, the value default size is 1M, so 1M size satisfies most page size storage.

There are other methods, such as template substitution method, Opcache, etc.

Transfer from http://www.jb51.net/article/59619.htm

PHP static page (reprint)

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.