PHP implementation of HTML page static method, phphtml page static
As the content of the site increases and user visits increase, it is inevitable that the site will load more and more slowly, limited by the bandwidth and the number of requests from the server at the same time, and we often need to optimize our site for code optimization and server configuration at this time.
In general, the following areas will be optimized
- Dynamic page Static
- Optimizing the Database
- Using load Balancing
- Using the cache
- Using CDN Acceleration
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 Web 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 the file, access speed can be imagined, and its search engine is also a very friendly way.
How does a purely static website be implemented in a Web site?
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, The generation needs to read the source file and then generate a separate page file ending in. html, so that the pure static site needs more space, but in fact, the space will not be much larger, especially for small and medium-sized enterprise website, from the technical point of view, large-scale web site to achieve pure static is more difficult, the generation of time is too long 。 However, small and medium-sized sites are still made purely static comparisons, the advantages of doing so are many.
And how is dynamic website static processing?
Page statics refers to turning dynamic pages into html/htm static pages. Dynamic pages are generally written by asp,php,jsp,.net and other programming languages, and are very easy to manage. However, access to the Web page requires the program to process it first, which results in a relatively slow access speed. The static page access speed is fast, but it is not easy to manage. Then dynamic page static can bring together the benefits of both pages.
What benefits does it bring to the site after static processing?
- Static pages are more easily indexed by search engines than dynamic pages.
- Accessing a static page does not need to be processed by the program, so it can improve speed of operation.
- Reduce the burden on the server.
- HTML pages are not affected by ASP-related vulnerabilities.
Static processing after the site is relatively non-static processing of the site is also more security, because the static site is not the preferred object of hacking, because hackers do not know your background system, hackers from the front of the static page is difficult to attack. Also has a certain stability, such as the database or the program of the site is a problem, he will not interfere with the static processing of the page, not because of the program or data impact can not open the page.
Search engine spider program prefers such URLs, can also alleviate the workload of the Spider program, although some people will think that the search engine is now fully capable to crawl and identify dynamic URLs, here or suggest that you can make static as far as possible static URLs.
Below we mainly talk about the concept of static page, I hope to help you!
What is HTML static:
Often said page static is divided into two kinds, one is pseudo-static , that is, url rewriting, one is true static .
In the development of PHP Web site for website promotion and SEO needs, need to site or local static processing, PHP generated static HTML page has a number of methods, such as the use of PHP templates, caching and other implementation of the page 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.
PHP pseudo-static: the method of using Apache mod_rewrite to implement URL rewriting.
The benefits of HTML static:
One, reduce the burden on the server, browse the Web without calling the system database.
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, speed up the page opening speed, static pages do not need to connect the database open faster than the dynamic page has significantly improved;
Four, the website is more secure, HTML page will not be affected by the PHP program related vulnerability; Looking at a larger site is basically a static page, and can reduce attacks and prevent SQL injection. When a database error occurs, it does not affect normal site access.
Five, the database error, does 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 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 the trouble, the program is complicated, but in order to better search, for faster, more secure, these sacrifices are worth.
Implementation of HTML static strategies and examples to explain:
Basic Way
File_put_contents () function
Use the PHP built-in caching mechanism to implement page static-output-bufferring.
Method 1: Generate a static page with a PHP template
PHP templates are easy to implement statically, such as installing and using PHP Smarty to make your site static.
In the case of using smarty, you can also implement page statics. Let's start with a brief look at the usual dynamic reading when using Smarty.
These steps are generally divided into:
1. Pass a parameter (ID) via URL;
2, then query the database based on this ID;
3, after obtaining the data, modify the display content as needed;
4, assign need to display the data;
5. Display template file.
The Smarty static process requires only two steps to be added in the above procedure.
First: Use Ob_start () to open the buffer before 1.
Second: After 5, use Ob_get_contents () to get the contents of 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 foreground of the website, and content management (add, modify, delete) is usually done in the background, in order to effectively use the above process, you can use a small means, that is the header (). The process is this: after the addition, modification of the program is completed, using the header () to skip to the foreground read, so that the page can be HTML, and then jump back to the backend management side after the HTML is generated, and the two jump process is not visible.
Method 2: Generate a static page using the PHP file read and write function
<? $out 1 = "PHP website Static Tutorial
Method 3: Generate a static page using the PHP output control function/OB caching mechanism
The output control function, which uses and controls the cache to generate static HTML pages, also uses the PHP file read-write function.
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 according to this address once, and then save as a static page, the next time someone accesses this product Details page dynamic address, we can directly to the generated corresponding static content file output.
php generate static page instance code 1
<? Ob_start (); echo "". "". "PHP website Static Tutorial". "". " Welcome to visit the home of help, this article mainly introduces the method of static page of PHP website. ""; $out 1 = ob_get_contents (); Ob_end_clean (); $fp = fopen ("leapsoulcn.html", "w"); if (! $fp) {echo "System Error"; exit ();} else {fwrite ($fp, $out 1); fclose ($FP); echo "Success";}?>
PHP generate static Page instance code 2
<?php $gid = $_get[' gid ']+0;//commodity id $goods _statis_file = "Goods_file_". $gid. HTML ";//corresponds to a 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 contents exit; } else{//If expired unlink ($goods _statis_file);//delete the 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 Details page template $content = ob_get_contents ();//Assign the details page content to $ Content variable file_put_contents ($goods _statis_file, $content);//write to the corresponding static file Ob_end_flush ();//Output Commodity 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 Details page template $content = ob_get_contents ();//Assign the details page content to $ Content variable file_put_contents ($goods _statis_file, $content);//write to the corresponding static file Ob_end_flush ();//Output Commodity Details page information }
We know that using PHP for Web development, the general implementation of the results of direct 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, in order to obtain the contents of the buffer, and then output to the static HTML page file to achieve the site static.
PHP generates static pages by first opening the cache, then outputting the HTML content (you can also include the HTML content as a file via include), and then getting the contents of the cache. After emptying the cache, the cached content is written to a static HTML paging file through a PHP file read-write function. 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 Point:
1, Ob_start function is mostly used to turn on the cache, pay attention to the use of ob_ You cannot have any output before start, such as spaces, characters, and so on. The
2, ob_get_contents function is primarily used to get the contents of the cache returned as a string, note that this function must be called before the Ob_end_clean function, otherwise it will not get cached content.
3, the Ob_end_clean function is mainly to empty the contents of the cache and close 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 Details page template $content = ob_get_contents ();//Assign the details page content to $ Content variables $mem->add ($goods _statis_content, $content, False, $expr); Ob_end_flush ();//Output Commodity details page Info }
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.
The above is the PHP implementation of the HTML page static method, rich content, it is worth our careful taste, from which to get harvest.
http://www.bkjia.com/PHPjc/1067827.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067827.html techarticle PHP Implementation of the HTML page static method, phphtml page static with the content of the site and the increase in user access, it is inevitable that the site load will be more and more slow, limited by the bandwidth ...