A simple way to implement page statics in PHP _php example

Source: Internet
Author: User
Tags nginx server

Why should the page be static?

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

2. Static file, do not need to compile, reduce the server script running time, reduce the response time of the server, direct operation, fast response; If some content in the page does not change frequently, dynamic page statics is a very effective acceleration method. (pure static, pseudo static or PHP interpreter required)

3, the generation of static URLs conducive to SEO, for spiders to crawl and included, conducive to the promotion of ranking

Optimizing page Response Time method

1. Dynamic page static

2. Optimizing the Database

3. Load Balancing

4. Using caching and so on

Dynamic page static is generally used in places that are not often changed, frequently changed places generally do not apply static, available pseudo static (such as micro-blog, etc.)

Static detailed description of

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 look, pseudo static and static page is the same. Pseudo-static page suffixes can be HTML htm or directory format

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

Static pages can save server resources, while pseudo static is strictly to increase server resource consumption

To summarize, in SEO aspect, pseudo static and static page function is same, but pseudo static is still dynamic page in nature, so consume resource is same with dynamic page, and because rewrite server also need to consume additional resources.

Buffer buffers Cognition

1. Open buffer

• Output_buffering Open in php.ini
• Use the Ob_start () function in the PHP file to open

; Default Value:off
; Development value:4096
; Production value:4096
http://php.net/output-buffering
output_buffering = 4096

2, get the contents of the buffer

Output_buffering=on need to be open before the ob_get_contents () function can be invoked. However, if output_buffering is not turned on, ob_get_contents () can 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 to implement a pure static page of PHP

Basic Way

1, File_put_contents

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

Ob_start ()//If the php.ini is turned on, a new output buffer will be opened here;
Ob_get_contents ()//Get output buffer content
, Ob_clean ()/Purge output buffer content, but not delete output buffer
ob_get_clean//get output buffer content and delete output buffer, equivalent to OB _get_contents and Ob_end_clean)

Below this code, running is not going to have output

The reason for this is that the output buffer is emptied, and the image is understood

Ob_start ();
Echo 777;
echo;
Ob_clean ();
Echo ob_get_contents ();

Pure static Implementation, code and Implementation Logic reference:

<?php/** * Trigger system generates pure static page business logic * There are 3 kinds of programs: * First: Timing scanner (using crontab to deal with) * Second: Manual trigger, artificial trigger * Third: the page to add cache time, in the page control time to operate */ /===========================================//Generate the Pure static file step//1, connect the database, then get the data from the database//2, fill the acquired data into the template file//3, Need to change the dynamic page to static page, generate static file//============================================//php implementation of the page static has the following steps://1: a.php Request database data: through MySQL or mysqli or PDO extensions///2: Output a.php requested database data in a.html: Typically, a new array is given in the form of an array taken out of the database, and Output//3: Include a.html files in a.php: Directly through the require_once () function or inclde_once ()//4: Turn on the data cache Ob_start () => get the cached content and generate the data in a static file 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) {//cache not invalidated) load static file directly require_once (
'./index.html ');
  else {///cache fails to regenerate//Introduce database link operations 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 ')//import Template file File_put_contents ('./index.html ', ob_get_contents ());//Generate Static file//ob_
Clean ();
 }

The implementation of local dynamic in static pages

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

Pseudo static

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

Apache pseudo static settings

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

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

2, the Inculde conf/extra/httpd-vhosts.conf virtual hosts support, fictitious domain name configuration

3. Edit Vartual Host File

4. Local host file to join the configured domain name (if native test is required for Windows)

5. Pseudo-Static configuration

-5.1 rewrite engine on
-5.2 Writing Rules

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

Put it in the VirtualHost section.

Written in post.php

<?php 
Echo ' is '. $_get[' id '];

Then you can access the a.com/123.html returned by this is 123.

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

Rewriteengine on
rewriterule ^/post/([0-9]*). html$/post.php?id=$1
#存在目录
rewritecond%{document_root}% {request_filename}!-d
#存在文件
rewritecond%{document_root}%{request_filename}}!-f

The above two words means if there is a directory or file in the root directory, then use his

Of course this should be placed on the top of the rewrite that just now.

Nginx pseudo Static

Pseudo-static is to affect the performance of the server, not the more the better, needs to be determined

The above PHP implementation of the static page of the super simple method is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.