PHP static HTML pages

Source: Internet
Author: User
Tags php website
This article mainly introduces PHP static HTML pages, shares the advantages of static processing methods and static processing, and provides various static methods, if you are interested, you can refer to the slow loading of websites as the website content increases and the number of user visits increases, due to the limited bandwidth and the number of requests at the same time on the server, we often need to optimize the code and server configuration of our website at this time.
Generally, optimization is performed from the following aspects:

  • Static dynamic page
  • Optimize database
  • Use server load balancer
  • Use cache
  • Use CDN acceleration

Currently, many websites need to be statically processed during construction. Why does the website need to be statically processed? We all know that a pure static website is an independent html page for all webpages. when we access the website, we can directly read the file without data processing, and the access speed can be imagined, it is also a friendly method for search engines.
How does a pure static network stand on a website?
The page file at the end of the website, so pure static websites need more space, but actually the space is not much, especially for small and medium-sized enterprise websites, technically speaking, it is difficult for large websites to achieve pure static content for the whole site, and the generation time is too long. However, small and medium websites are still compared with pure static websites, which has many advantages.

How does a dynamic website perform static processing?
Static pages refer to converting dynamic pages into html/htm static pages. Dynamic pages are generally compiled by programming languages such as asp, php, jsp, and. net, which is easy to manage. However, when accessing a webpage, the program must first process the webpage, resulting in a relatively slow access speed. Static page access is fast but not easy to manage. Static dynamic pages can combine the benefits of the two pages.

What benefits does static processing bring to websites?

  • Static pages are more easily indexed by search engines than dynamic pages.
  • Accessing static pages does not need to be processed by a program, so it can improve the running speed.
  • Reduce the burden on the server.
  • HTML pages are not affected by Asp vulnerabilities.

Websites with static processing are relatively secure compared to websites without static processing, because static websites are not the first choice for hacker attacks, it is difficult for hackers to launch attacks from static pages on the front-end without knowing your background system. At the same time, it also has a certain degree of stability. for example, if a database or website program has a problem, it will not interfere with the page after static processing, and will not open the page because of the impact of the program or data.

Search engine spider programs prefer such websites, which can also reduce the workload of spider programs. although some people think that the search engine is capable of capturing and identifying dynamic websites, we recommend that you make static URLs as much as possible.

Next we will mainly talk about the concept of page static, and hope to help you!
What is HTML static:

The static page can be divided into two types:Pseudo-static, That is, url rewriting.True static.
In PHP website development, full-site or partial static processing is required for website promotion and SEO. PHP provides multiple methods to generate static HTML pages, for example, static pages are implemented using PHP templates and cache.
PHP staticIt means that pages generated by websites are displayed in the form of static HTML in front of visitors. PHP static is divided into pure static and pseudo static. The difference between PHP and PHP is that the processing mechanism for generating static pages is different.
PHP pseudo-static:Use Apache mod_rewrite to implement URL rewriting.

Benefits of static HTML:

I,Reduces the burden on the server. you do not need to call the system database to browse the web page.
II,It is conducive to SEO optimization by search engines. both Baidu and Google will give priority to static pages, not only quick but also full;
III,Speed up page opening. static pages do not need to be connected to databases. the speed of opening static pages is significantly higher than that of dynamic pages;
IV,Websites are more secure. HTML pages are not affected by vulnerabilities related to php programs. a bigger website is basically a static page, which can reduce attacks and prevent SQL injection. When a database error occurs, normal website access is not affected.
V,When a database error occurs, normal website access is not affected.
The most important thing is to increase access speed and reduce the server load. when the data volume is tens of thousands, hundreds of thousands, or more, you know which one is faster and is easy to find by search engines. Although generating html articles is difficult to operate and complicated in the program, these sacrifices are worth the effort to make it easier for search and make it faster and safer.

Static HTML policies and examples:
Basic method
File_put_contents () function
Use php built-in Cache mechanism to achieve static page-output-bufferring.

Method 1: use PHP templates to generate static pages

It is very convenient to implement static PHP templates, such as installing and using PHP Smarty to implement static websites.
When Smarty is used, the page can also be static. The following describes how to use Smarty to dynamically read data.
Generally, the following steps are taken:
1. pass a parameter (ID) through the URL );
2. query the database based on this ID;
3. after obtaining the data, modify the display content as needed;
4. data to be displayed for assign;
5. display template file.
In the static process of Smarty, you only need to add two steps in the above process.
First, use ob_start () to open the buffer before 1.
Second, use ob_get_contents () after 5 to get the content not output in the memory, and then use fwrite () to write the content to the target html file.

According to the preceding descriptions, this process is implemented on the website's front-end, while content management (add, modify, and delete) is usually performed on the background to make effective use of the above process, you can use a small method, that is, Header (). The specific process is as follows: after adding or modifying a program, use Header () to jump to the foreground to read the program. in this way, the page can be HTML-based, and then jump back to the background management side after html is generated, the two jump processes are invisible.

Method 2: Use the php file read/write function to generate a static page

<? $ Out1 ="PHP static website tutorialWelcome to www.leapsoul.cn for PHP website development. This article mainly introduces the static PHP website page method."; $ Fp = fopen (" leapsoulcn.html "," w "); if (! $ Fp) {echo "System Error"; exit () ;}else {fwrite ($ fp, $ out1); fclose ($ fp); echo "Success" ;}?>

Method 3: Use the PHP Output Control function (Output Control)/ob Cache mechanism to generate static pages
The Output Control function uses and controls the cache to generate static HTML pages. It also uses the php file read/write function.
For example, the dynamic details page address of a product is: http://xxx.com? Goods. php? Gid = 112
Here we read the content of this Details page based on this address and save it as a static page. The next time someone accesses the dynamic address of this product details page, we can directly output the generated static content file.
PHP generates static page instance code 1

<? Ob_start (); echo"".""."PHP static website tutorial".""."Welcome to the home of scripts. This article mainly introduces the static PHP website page method.".""; $ Out1 = ob_get_contents (); ob_end_clean (); $ fp = fopen (" leapsoulcn.html "," w "); if (! $ Fp) {echo "System Error"; exit () ;}else {fwrite ($ fp, $ out1); fclose ($ fp); echo "Success" ;}?>

PHP generates static page instance code 2

<? Php $ gid = $ _ GET ['gid'] + 0; // item id $ goods_statis_file = "goods_file _". $ gid. ". html "; // corresponding static page file $ expr = 3600*24*10; // static file validity period, ten days if (file_exists ($ goods_statis_file )) {$ file_ctime = filectime ($ goods_statis_file); // if ($ file_ctime + $ expr --> time ()) {// echo file_get_contents ($ goods_statis_file); // output static file content exit;} else {// If unlink ($ goods_statis_file) has expired ); // delete the expired static page file ob_start (); // read data from the database and assign the value to the relevant variable // in Clude ("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 the content to the corresponding static file ob_end_flush (); // output the product details page information} else {ob_start (); // read data from the database, and assign the value to the relevant variables // include ("xxx.html"); // load the corresponding product details page template $ content = ob_get_contents (); // assign the content on the details page to the $ content variable file_put_contents ($ goods_statis_file, $ content); // write the content to the corresponding static file ob_end_flush (); // output product details page information}?>


We know that PHP is used for website development. Generally, the execution results are directly output to the browser. to generate a static page using PHP, you need to use the output control function to control the cache to obtain the content in the cache, and then output to the static HTML page file to achieve static website.

The idea of generating static pages in PHP is: first enable caching, then output HTML content (you can also include HTML content in the form of a file through include), and then get the content in the cache, after the cache is cleared, the PHP file read/write function writes the cached content to the static HTML page file.
The process of obtaining the output cache content to generate a static HTML page requires three functions: ob_start (), ob_get_contents (), and ob_end_clean ().

Knowledge Point:
1. the ob_start function is generally used to enable caching. Note that no output, such as spaces or characters, is allowed before the ob_start function is used.
2. the ob_get_contents function is mainly used to obtain the cached content and return it as a string. Note that this function must be called before the ob_end_clean function; otherwise, the cached content cannot be obtained.
3. the ob_end_clean function clears the content in the cache and closes the cache. if the cache is successful, True is returned. if the cache fails, False is returned.
Method 4: Use nosql to read content from memory (in fact, this is not static but cached );
Take memcache as an example:

<? Php $ gid = $ _ GET ['gid'] + 0; // item id $ goods_statis_content = "goods_content _". $ gid; // key $ expr = 3600*24*10; // validity period, 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 data from the database, and assign the value to the relevant variables // include ("xxx.html"); // load the corresponding product details page template $ content = ob_get_contents (); // assign the details page content to $ cont Ent variable $ mem-> add ($ goods_statis_content, $ content, false, $ expr); ob_end_flush (); // output product details page information}?>

Memcached is a one-to-one correspondence of key values. the default key size cannot exceed 128 bytes, and the default value size is 1 M. Therefore, the size of 1 M meets the requirements of storage for most webpages.

The above is the method for PHP to achieve static HTML pages. the content is rich and deserves your taste and gains.

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.