PHP implementation of HTML page static method _php skills

Source: Internet
Author: User
Tags html page php template php website sql injection

With the increase of the content of the website and the increase of user's traffic, it is unavoidable that the load of the website will be more and more slow, limited by the bandwidth and the number of requests of the server at the same time, we often need to optimize the code and server configuration of our website at this time.
In general, it will be optimized from the following aspects

    • Dynamic page Static
    • Optimizing the Database
    • Using load Balancing
    • Using caching
    • Using CDN to accelerate

Now many sites in the construction of the time to be static processing, why the site to be static processing it? We all know that the pure static site is all the pages are independent of an HTML page, when we visit the time does not need to go through the processing of data directly can read to the file, access speed can be imagined, and its search engine is also a very friendly way.
How does a pure static website implement in the website?
Pure static production technology is the need to first summarize the site page, divided into how many styles, and then make these pages into a template, When you build, you need to read the source file and then generate a separate page file that ends in. html, so that pure static site needs more space, but in fact, the need for space is not much, especially for small and medium enterprises, from the technical point of view, large Web sites want to achieve pure static is more difficult, the generation of time is too long 。 However, small and medium-sized sites are still made of pure static comparison, the advantages of doing so are many.

And how the dynamic site is static processing?
page static refers to the dynamic page into a html/htm static page. Dynamic pages are generally written by asp,php,jsp,.net and other programming languages, and are very easy to manage. However, when you visit a Web page, you need to process it first, which results in a relatively slow access rate. and static page access speed, but not easy to manage. Then dynamic page static can bring the benefits of both kinds of pages together.

What benefits does static processing bring to the website?

    • Static pages are more likely to be indexed by search engines relative to dynamic pages.
    • Access to static pages does not require program processing, so you can increase the speed of running.
    • Reduce server burdens.
    • HTML pages are not affected by ASP-related vulnerabilities.

Static processing of the site is relatively not static processing of the site is also more security, because static Web site is not the preferred target hacker attacks, because hackers do not know your background system, hackers from the front of the static page is difficult to attack. At the same time also has a certain degree of stability, such as the database or Web site program out of the problem, he will not interfere with the static processing of the page, will not be due to the program or data impact and can not open the page.

Search engine spider program prefers such URLs, can also reduce the workload of the Spider program, although some people will think that now the search engine is fully capable to crawl and identify Dynamic Web site, here or suggest that you can make static as far as possible static web site.

Below we mainly talk about the concept of static page, I hope to help you!
What is HTML static:

Often said that the page static is divided into two kinds, one is pseudo static , that is, url rewrite, one is true static .
PHP Web site development in order to promote and SEO needs, the site needs to be a whole station or local static processing, PHP generated static HTML page has a variety of methods, such as the use of PHP templates, caching and other implementation of the page static.
the simple understanding of PHP static is to make the site generated page in the form of static HTML display in front of visitors, PHP static separation of pure static and pseudo static, the difference between the two is that PHP generated static page processing mechanism is different.
PHP pseudo-static: a way to implement URL rewriting using Apache mod_rewrite.

The benefits of HTML statics:

first, reduce the server burden, browsing the Web page without calling the system database.
second, conducive to search engine optimization Seo,baidu, Google will be a priority to include static pages, not only included in the fast also included the whole;
third, speed up the page opening speed, static pages do not need to connect the database to open faster than dynamic pages have significantly improved;
Four, the site is more secure, the HTML page will not be affected by the PHP program-related vulnerabilities; Watch a larger site is basically static page, and can reduce attacks, prevent SQL injection. When a database error occurs, it does not affect normal site access.
when the database error, do not affect the normal access to the site.
The main thing is to increase the speed of access, reduce the burden on the server, when the amount of data is tens of thousands of, hundreds of thousands of or more when you know which is faster. But also easy to be found by search engines. Generate HTML article Although the operation of some trouble, the program is more complicated, but in order to more conducive to search, in order to faster, more secure, these sacrifices are worthwhile.

Strategies and examples for implementing HTML statics:
Basic Way
File_put_contents () function
Use PHP built-in caching mechanism to implement page static-output-bufferring.

Method 1: Use PHP template to generate static pages

PHP templates to achieve static is very convenient, such as the installation and use of PHP Smarty implementation of the Web site static.
In the case of using smarty, you can also implement page statics. Let's say a little bit about the way you normally read dynamically when using Smarty.
generally divided into these steps:
1. Pass a parameter (ID) by URL;
2, then query the database according to this ID;
3, after obtaining the data according to need to modify the display content;
4, assign need to display the data;
5, display template file.
The Smarty static process requires only two steps to be added to the above procedure.
First: Use Ob_start () to open the buffer before 1.
Second: Use Ob_get_contents () after 5 to get the memory not output, and then use Fwrite () to write the content to the destination HTML file.

According to the above description, this process is implemented in the site foreground, and content management (add, modify, delete) is usually in the background, in order to be able to effectively use the above process, you can use a little bit of means, that is header (). The specific process is this: after adding, modifying the program is completed, use header () to jump to the foreground read, so that the page can be HTML, and then after the generation of HTML to jump back to the background management side, and these two jump process is not visible.

Method 2: Use PHP file read and write function to generate static page

? 
$out 1 = " 
 

Method 3: Generate a static page using the PHP output control function/OB caching mechanism
output control functions, which use and control caching to generate static HTML pages, also use the PHP file read-write function.
For example, a product dynamic details page address is: http://xxx.com?goods.php?gid=112
So here we read the content of this detail page from this address once, then save it as a static page, and the next time someone accesses the dynamic address of the Product Details page, we can directly output the corresponding static content file that has been generated.
PHP generates static page instance code 1

? 
Ob_start (); 
echo " 
 

PHP generates static page instance code 2

 <?php $gid = $_get[' gid ']+0;//commodity id $goods _statis_file = "Goods_file_". $gid. ". HTML "//corresponds to the static page file $expr = 3600*24*10;//static file validity, 10 days if (file_exists ($goods _statis_file)) {$file _ctime =filectime ($goods _ Statis_file)//File creation time if ($file _ctime+ $expr-->time ()) {//If not expired echo file_get_contents ($goods _statis_file); 
 Output static file content exit; 
 
  }else{//If expired unlink ($goods _statis_file);//delete expired static page file Ob_start (); 
  Read the data from the database and assign the value to the relevant variable//include ("xxx.html");//load the corresponding Product detail 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 ()//Output details page information}}else{Ob_start (); Read the data from the database and assign the value to the relevant variable//include ("xxx.html");//load the corresponding Product detail page template $content = ob_get_contents ()//Assign the details page content to $content variable fi Le_put_contents ($goods _statis_file, $content)//write content to the corresponding static file Ob_end_flush ()//Output Product Details page information}?> 


We know that the use of PHP for Web site development, the general results directly output to the browser, in order to use PHP to generate static pages, you need to use the output control function to control the buffer cache to obtain the contents of the buffer, and then output to the static HTML page file to achieve static Web site.

PHP generated a static page thinking: First open the cache, and then output the HTML content (you can also include HTML content in the form of file), and then get the contents of the cache, empty the cache through the PHP file read and write function to write the cached content to the static HTML page file.
The process of getting the cached content of the output to generate a static HTML page requires three functions: Ob_start (), ob_get_contents (), Ob_end_clean ().

Knowledge Points:
1, Ob_start function is generally used to open the cache, pay attention to the use of Ob_start can not have any output, such as spaces, characters and so on.
2, the Ob_get_contents function is mainly used to get the contents of the cache to return as a string, note that this function must be called before the Ob_end_clean function, otherwise the cached content is not obtained.
3, the Ob_end_clean function is mainly empty the contents of the cache and turn off the cache, success returns TRUE, Failure returns false
Method 4: Use NoSQL to read content from memory (in fact, this is not static but caching);
take Memcache as an example:

<?php 
$gid = $_get[' gid ']+0;//commodity id 
$goods _statis_content = "Goods_content_". $gid//corresponding key 
$expr = 3600* 24*10;//valid, 10 days 
$mem = new Memcache; 
$mem--->connect (' memcache_host ', 11211); 
$mem _goods_content = $mem->get ($goods _statis_content);  
if ($mem _goods_content) { 
 echo $mem _goods_content; 
} else{ 
 Ob_start (); 
 
 Read the data from the database and assign the value to the relevant variable 
 
 //include ("xxx.html");//load the corresponding Product detail page template 
 
 $content = ob_get_contents ()//Assign the details page contents to $ Content variables 
 $mem->add ($goods _statis_content, $content, False, $expr); 
 Ob_end_flush ()//Output Product Details page Information 
 
} 
 
?> 

Memcached is a key value of one by one corresponding, key defaults to the maximum of 128 bytes, value default size is 1M, so 1M size to meet the majority of the size of the Web page storage.

The above is the PHP implementation of HTML page static method, rich in content, it is worth everyone to savor, from which harvest.

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.