In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "PHP how to achieve HTML page static", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "PHP how to achieve HTML page static" this article.
In general, optimization will be made from the following aspects
Static dynamic page
Optimize the database
Use load balancin
Use cach
Using CDN acceleration
Now many websites have to be statically processed during construction, so why should websites be statically processed? We all know that a pure static website is that all pages are independent html pages. When we visit a website, we can read the file directly without data processing, and the access speed can be imagined, and it is also a very friendly way for search engines.
How is the pure static website realized in the website?
The pure static production technology is that you need to summarize the pages of the website, divide them into how many styles, and then make these pages into templates. When generating, you need to read the source file first and then generate an independent page file ending in .html. So pure static websites need more space, but in fact, the space will not be much larger, especially for small and medium-sized enterprise websites. It is difficult for large websites to achieve pure static in the whole site, and the generation time is too long. However, small and medium-sized websites are still made into pure static comparison, which has a lot of advantages.
And how do dynamic websites deal with them statically?
Page static refers to turning a dynamic page into a html/htm static page. Dynamic pages are generally written by programming languages such as asp,php,jsp,.net, which is very easy to manage. However, when visiting a web page, the program still needs to deal with it first, so the access speed is relatively slow. On the other hand, the static page access speed is fast, but it is not easy to manage. Then static dynamic pages can bring together the benefits of the two kinds of pages.
What benefits do static processing bring to the website?
Static pages are easier to be included by search engines than dynamic pages.
Accessing static pages does not need to be processed by the program, so it can be run faster.
Reduce the burden on the server.
HTML pages are not affected by Asp-related vulnerabilities.
The website after static processing is more secure than the website without static processing, because the static website will not be the preferred target for hackers, because the hackers do not know your background system. It is difficult for hackers to attack static pages from the foreground. At the same time, it also has a certain degree of stability, such as database or website program problems, it will not interfere with the static processing of the page, will not be affected by the program or data can not open the page.
Search engine spider programs prefer such URLs, and can also reduce the workload of spider programs, although some people will think that search engines are now fully capable of crawling and identifying dynamic URLs. Here, we still suggest that we can make static web addresses as far as possible.
Next we will mainly talk about the concept of page static, I hope it will help you!
What is HTML static:
Often said that the page static is divided into two kinds, one is pseudo-static, that is, url rewriting, and the other is true static.
In the development of PHP website, for the needs of website promotion and SEO, the website needs to be static or partially static. There are many ways for PHP to generate static HTML pages, such as using PHP templates, caching and so on to achieve static pages.
The simple understanding of PHP static is to make website generated pages displayed in front of visitors in the form of static HTML. PHP static is divided into pure static and pseudo-static. The difference between the two lies in the different processing mechanism of PHP generated static pages.
PHP pseudo-static: a method of using Apache mod_rewrite to implement URL rewriting.
Benefits of HTML quiescence:
First, lighten the burden on the server and browse the web without calling the system database.
Second, conducive to search engine optimization SEO,Baidu, Google will give priority to include static pages, not only included fast but also included all
Third, speed up the opening speed of the page, and the opening speed of static pages without database connection is significantly higher than that of dynamic pages.
Fourth, the website is more secure, HTML pages will not be affected by php program-related vulnerabilities; take a look at the larger website is basically all static pages, and can reduce attacks, prevent sql injection. When there is an error in the database, the normal access to the website will not be affected.
Fifth, when there is an error in the database, it will not affect the normal access to the website.
The most important thing is to increase the access speed and lighten the burden on the server. When there are tens of thousands, hundreds of thousands or more of data, you know which is faster. And it is easy to find by search engines. Although the operation of generating html articles is more troublesome and the program is more complicated, it is worth the sacrifice to facilitate the search, to be faster and safer.
Strategies and examples to achieve static HTML are explained as follows:
Basic mode
File_put_contents () function
Use php built-in caching mechanism to achieve page static-output-bufferring.
Method 1: generate static pages using PHP templates
Static implementation of PHP templates is very convenient, such as installing and using PHP Smarty to achieve website static.
In the case of using Smarty, you can also make the page static. Let's start with a brief description of the usual practice of dynamic reading when using Smarty. ?
It is generally divided into the following steps:
1. Pass a parameter (ID) through URL
2. Then query the database according to this ID
3. Modify the display content as needed after obtaining the data
4. Data to be displayed in assign
5. Display template file.
The Smarty static process only needs to add two steps to the above process.
First: use ob_start () to open the buffer before 1.
Second: after 5, use ob_get_contents () to get the unoutput content of memory, and then use fwrite () to write the content to the target html file.
?
According to the above description, this process is implemented in the foreground of the site, while content management (add, modify, delete) is usually carried out in the background, in order to effectively take advantage of the above process, you can use a small means, that is, Header (). The specific process is like this: after adding and modifying the program, use Header () to jump to the foreground to read, so that you can achieve the HTML of the page, and then jump back to the background management side after generating html, and the two jump processes are not visible.
Method 2: use PHP file read and write function to generate static pages
Method 3: use PHP output control function (Output Control) / ob cache mechanism to generate static pages
The output control function (Output Control), which uses and controls the cache to generate static HTML pages, also uses the PHP file read and write function.
For example, the dynamic details page address of a product is: http://xxx.com?goods.php?gid=112
So here we read the contents of this detail page once according to this address, and then save it as a static page. The next time someone visits the dynamic address of this product details page, we can directly output the corresponding static content file that has been generated.
PHP generates static page instance code 1
PHP generates static page example code 2
?
We know that using PHP for website development, the general implementation results are directly output to the browser, in order to use PHP to generate static pages, we need to use the output control function to control the cache area in order to get the contents of the cache area, and then output to the static HTML page file to achieve website static.
The idea for PHP to generate a static page is as follows: first, open the cache, and then output the HTML content (you can also include the HTML content in a file through include), then get the contents in the cache, and then write the cached content to the static HTML page file through the PHP file read-write function after clearing the cache.
The process of getting the cached content of the output to generate a static HTML page requires three functions: ob_start (), ob_get_contents (), and ob_end_clean ().
?
Knowledge points:
1. The ob_start function is mainly used to open cache. Note that you cannot have any output, such as spaces, characters, etc., before using ob_start.
2. The ob_get_contents function is mainly used to get the content in the cache and return it as a string. Note that this function must be called before the ob_end_clean function, otherwise the cache content cannot be obtained.
3. The ob_end_clean function mainly clears the contents of the cache and closes the cache. If it succeeds, it returns True, and if it fails, it returns False.
? Method 4: use nosql to read from memory (in fact, this is no longer static but cached)
Take memcache as an example:
Memcached is an one-to-one correspondence between keys and values. By default, the maximum size of key cannot exceed 128bytes, and the default size of value is 1m, so the 1m size meets the storage requirements of most web pages.
These are all the contents of the article "how to make HTML pages static in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.