Go A super-simple way to implement page static in PHP

Source: Internet
Author: User
Tags nginx server

Why are pages static?

1. Dynamic file Execution Process: parsing-compiling-running

2. Static files, do not need to compile, reduce the server script run time, reduce the response time of the server, direct operation, fast response; Dynamic page static is a very effective acceleration method if some content in the page changes infrequently. (pure static, pseudo-static or PHP interpreter required)

3, generate static URLs to facilitate SEO, spiders crawl and ingest, to promote the ranking

Optimize page Response Time method

1. Dynamic page static

2. Optimizing the Database

3. Load Balancing

4. Using cache and so on

Dynamic page static is generally used in places where changes are not often, where frequent changes are generally not applicable to static, pseudo-static (such as Weibo, etc.)

Detailed description of static

1, pure static is divided into local static (local dynamic, using AJAX dynamic acquisition of data) and pure static.

Pseudo-static: Change URL (requires server support, such as Apache, etc.)

2, from the URL structure and page name, pseudo-static and static pages are the same. Pseudo-static page suffixes can be HTML htm or directory format

Pseudo-static only changes the expression of the URL, in fact, the dynamic page

Static pages can save server resources, while pseudo-static is strictly said to increase the consumption of server resources

Summary, in terms of SEO, pseudo-static and static page function is the same, but pseudo-static is essentially a dynamic page, so the consumption of resources is the same as the dynamic page, and because the rewrite server also needs to consume additional resources.

Buffer buffers Cognition

1. Open buffer

? output_buffering Open in PHP.ini
? Use the Ob_start () function in a PHP file to open

?
12345 ; Default Value: Off; Development Value: 4096; Production Value: 4096; http://php.net/output-bufferingoutput_buffering = 4096

2. Get the contents of the buffer

Output_buffering=on needs to be opened before the ob_get_contents () function can be called. However, if output_buffering is not turned on, ob_get_contents () can also be used when the function Ob_start () function is called in the header file.

Ob_get_content ();//Returns the contents of the output buffer;

How PHP implements pure static page

Basic Way

1, File_put_contents

2, the use of PHP built-in caching mechanism to achieve page static output_buffering

?
1234 ob_start()//如果php.ini已经开启,那么这里会开启一个新的输出缓冲区;ob_get_contents()//获取输出缓冲区内容;ob_clean()//清空输出缓冲区内容,但是不会删除输出缓冲区ob_get_clean//获取输出缓冲区内容并且删除输出缓冲区,等价于ob_get_contents和ob_end_clean)

The code below, there is no output to run

The reason is that the output buffer is emptied, see understanding

?
12345 ob_start();echo 777;echo 000;ob_clean();echo ob_get_contents();

Pure static Implementation, code and Implementation Logic reference:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 <?php/** * 触发系统生成纯静态化页面业务逻辑 * 有3种方案:  * 第一:定时扫描程序(利用crontab来处理)  * 第二:手动触发方式,人为触发 * 第三:页面添加缓存时间,在页面中控制时间来操作*///===========================================//生成纯静态文件步骤//1、连接数据库,然后从数据库里面获取数据//2、把获取到的数据填充到模版文件里面//3、需要把动态的页面转为静态页面,生成静态化文件//============================================//PHP实现页面静态化有以下步骤://1:A.php请求数据库数据:通过mysql或者mysqli或者PDO扩展//2:在A.html中输出A.php请求的数据库数据:一般是将将在数据库中取出的数组形式的数据赋予新的数组,并且输出//3:在A.php中包含A.html文件:直接通过require_once()函数或者inclde_once()//4:开启数据缓存ob_start()=>获取获取缓存内容并且将数据生成在静态文件中file_put_contents(‘index.shtml‘,ob_get_clean());//header("content-type:text/htm;charset=utf-8");if(is_file(‘./index.html‘) && (time() - filemtime(‘./index.html‘) < 1200)){  //缓存未失效则直接加载静态文件  require_once(‘./index.html‘);}else{  //缓存失效了则重新生成  // 引入数据库链接操作  require_once(‘./db.php‘);  $sql = "select * from news where `category_id` = 1 and `status` = 1 limit 4";  try  {      $db = Db::getInstance()->connect();      $result = mysql_query($sql, $db);      $newsList = array();      while($row = mysql_fetch_assoc($result))       {          $newsList[] = $row;      }  }  catch(Exception $e)  {      // TODO  }  ob_start();  require_once(‘template/index.php‘);//引入模版文件  file_put_contents(‘./index.html‘, ob_get_contents());//生成静态文件  //ob_clean();}

Local dynamic implementation of static pages

Using the AJAX request file in jquery, get the JSON data returned and apply it to the template.

Pseudo-Static

Nginx server does not support path info mode by default and requires additional configuration

Apache Pseudo-Static settings

1, open Apache mod_rewrite.so configuration in httpd.conf.

Test can be viewed with phpinfo, see if loaded modules has this module

2. Inculde conf/extra/httpd-vhosts.conf Virtual hosts support, VPN configuration

3. Edit Vartual Host File

4. The local host file is added to the configured domain name (if you need native testing for Windows)

5, pseudo-static configuration

-5.1 rewrite engine on
-5.2 Writing Rules

?
1 ^/post/([0-9]*).html$ /post.php?id=$1

Put it in the VirtualHost section.

Written in post.php

?
12 <?php echo‘this is ‘.$_GET[‘id‘];

You can then access the a.com/123.html returned by this is 123.

Extension: If the directory has 123.html of this real file, then the dynamic post 123 is still loaded.
So how to set up, if you want the current file to have a real static file, then you need the following configuration

?
123456 RewriteEngine onRewriteRule ^/post/([0-9]*).html$ /post.php?id=$1#存在目录RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-d#存在文件RewriteCond%{DOCUMENT_ROOT}%{REQUEST_FILENAME}}!-f

The above two sentences means if the root directory has the requested directory or file, then use his

This, of course, is on top of the rewrite just now.

Nginx Pseudo-Static

Pseudo-Static is the impact of server performance, not the more the better, need to be based on demand

The above this PHP implementation of the page static super simple method is to share all the content of the small part, I hope to give you a reference, but also hope that we support the script home.

Go A super-simple way to implement page static in PHP

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.