Using PHP to achieve static

Source: Internet
Author: User
Tags php website
Using PHP to achieve static

We in the development process of the PHP website for the promotion of the site or the need for SEO, the site needs to be a certain static, the design here to what is static page, so-called static page, not the page does not have animation and other elements, but refers to the page code is in the page, That is, do not need to execute PHP scripts and other server-side language to run, we can directly access to the page, which is static Web page.

So what are the benefits of static Web pages? The first main reason is because the search engine, because the search engine on the PHP page search for deer and HTML page contains a certain difference, and face the problem of page resource occupancy, we need to static. php files. One way is to rewrite the access address, you can modify it through the pathinfo mode of the URL, so that it looks more like a static page, which has a greater chance of being crawled and indexed by the search engine.

The 2nd reason is that it can facilitate the loading of the page, sometimes we go to some such as Sina, NetEase, the homepage of these sites, found that the content is very much, but its load time is really not long, which also has static credit. The website can be static by a certain program before the user visits the website, generate static page, when the user visits the page, because the access is static page, so, access speed is much faster than the speed of access to dynamic pages. This technique is necessary for large websites and can be used for small sites. Its performance in the foreground is the page loading speed, in the background of the performance is to reduce the database connection, reduce the pressure of the database, the only disadvantage is that the relative number of hard disk, but the hard disk is relatively inexpensive.

Now that you know some of the advantages of static, how do you do it statically? The static we can do with PHP is divided into pure static and pseudo-static, but not in the Bible static page mechanism is different, pseudo-static is to parse the URL and use rewrite mode to run the dynamic page, it is only a friendly search engine, not the true meaning of static, Let's introduce the pure static.

The so-called pure static, is the way to generate HTML files, we need to open PHP's own caching mechanism, that is, Ob_start to open the cache, and before Ob_start can have no output, otherwise the execution fails, and then we use ob_get_ Contents function to get the contents of the cache, the function returns a string, and the third function is Ob_end_clean, which is used to empty the contents of the cache and close, successfully returns true, and the failure returns false.

Here's a look at the example:

Here we extract the data from the database and then cache the data into the page, and here is the PHP file:

 
After we run the above page, we find that the folder automatically has an HTML file, the following is its code:

Array (size=6)0 =Array (size=4)' id ' =string' 1 '(length=1)' Level ' =string' 0 '(length=1)' Name ' + =string' Sinsing '(length=6)' PWD ' =string' bd04fcc97578ce33ca5fb331f42bc375 '(length=32)1 =Array (size=4)' id ' =string' 2 '(length=1)' Level ' =string' 1 '(length=1)' Name ' + =string' Xiao Qian '(length=6)' PWD ' =string' 61cb72858be523b9926ecc3d7da5d0c6 '(length=32)2 =Array (size=4)' id ' =string' 3 '(length=1)' Level ' =string' 1 '(length=1)' Name ' + =string' Little Nan '(length=6)' PWD ' =string' a3d2de7675556553a5f08e4c88d2c228 '(length=32)3 =Array (size=4)' id ' =string' 4 '(length=1)' Level ' =string' 1 '(length=1)' Name ' + =string' Liu Qiang '(length=6)' PWD ' =string' Fcdb06a72af0516502e5fdccc9181ee0 '(length=32)4 =Array (size=4)' id ' =string' 5 '(length=1)' Level ' =string' 1 '(length=1)' Name ' + =string' Star brother '(length=6)' PWD ' =string' 866a6cafcf74ab3c2612a85626f1c706 '(length=32)5 =Array (size=4)' id ' =string' 6 '(length=1)' Level ' =string' 1 '(length=1)' Name ' + =string' Sin Yong '(length=6)' PWD ' =string' E93beb7663f3320eaa0157730d02dd0c '(length=32)

Of course, this code is written by our PHP program automatically generated, can be accessed directly by the browser, thereby reducing the pressure on the database.


  • Related Article

    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.