Static page

Time:2020-11-22

1、 Introduction to static web page

1. Overview

It is to change a dynamic page (. PHP) into a static page (. HTML), and subsequent users can directly access the static page.

Static page

Page static technology is divided into two types: true static and pseudo static.

True static: turn a dynamic page into a static page, that is. HTML file

Pseudo static:The so-called pseudo static is a static page from the URL address, but it actually corresponds to a dynamic page

2. Several important concepts

(1) Dynamic website:

The so-called dynamic web address is generally used to query the database, such as: http://www.abc.com/goods.php?…

Features: query database, slow speed; receive parameters, security should pay attention to (SQL injection); is not conducive to seo search engine optimization.

(2) Static web address

For example: http://www.abc.com/index.htm… :

Features: no query database, fast speed; no receiving parameters, high security; conducive to SEO

(3) Pseudo static website address:

Formally, it is a static page, but actually it corresponds to a dynamic page,

Features: it needs to query the database, the execution speed is slow; it does not receive parameters, so it is safe; it is conducive to SEO

3. True static implementation

True static: using ob cache technology to achieve,

Pseudo static: using the rewrite mechanism of web server (URL rewriting mechanism) to achieve.

4. Introduction to ob

OB is output_ Buffering output cache. In the process of requesting a PHP, we actually go through three caches:

Program cache

OB cache

Browser cache

1. Program cache (on by default, cannot be turned off)

The cache is inherent in PHP and cannot be closed. The cache is returned to the client (browser side includesHeader informationAnd responseSubject information)Data.

2. OB cache (on by default, can be turned off)

OB cache (output_ Buffering) is also called ob bufferingCache the data of the response body

Opening mode:

(1) By using the function ob in the page_ Start(), if you use this function, it is only valid for the current page.

(2) Through php.ini Configuration file to open. output_ buffering = 4096

Static page

4096 is to set the capacity of OB cache. You can also open it without specifying the capacity, and use on

output_ Buffering = on | off | specific capacity


When a PHP file is requested, it actually passes through three buffers,

OB cache (if on) — “program cache -” browser cache.

If the OB cache is opened,Subject dataFirst, it is stored in the OB cache,The header information is stored in the program cache(whether ob cache is enabled or not),

After the code is executed, the data in the OB cache is refreshed (moved) to the program cache, and then the program cache is output to the browser cache, and finally the content is output.

Static page

5. Functions corresponding to ob cache

ob_start();//Enable the OB cache, which is valid on the current page

ob_get_contents();//Get the data in the OB cache.

ob_start();
echo '111';
echo '222';
$ob = ob_get_contents();
echo $ob;

Static page

ob_clean();Clear the data in the OB cache and do not close the OB cache.

ob_start();
echo '111';
echo '222';
$ob = ob_get_contents();
echo $ob;
ob_clean();
echo 'ok';

Static page

ob_end_clean();Clear the data in the OB cache and close the OB cache.

ob_start();
echo '111';
echo '222';
$ob = ob_get_contents();
echo $ob;
ob_end_clean();
echo 'ok';
header('content-type:text/html;charset=utf8')

Static page

Ob_ Flush(); / / refresh (move) the data in the OB cache to the program cache without closing the OB cache.

Ob_ Start(); // enable ob cache
echo '222';

Ob_ Flush(); // move the data in the OB cache to the program cache without closing the OB cache;
echo 'ok';
header('content-type:text/html;charset=utf8');

Static page

Ob_ end_ Flush(); / / refresh (move, push) the data in the OB cache to the program cache, and close the OB cache.

Ob_ Gzhandler(); / / compresses the data, depending on the compression format that the browser can accept, forming different compression codes.

ob_start(“ob_gzhandler”);The data in ob cache can be compressed and returned.

Commonly used are:ob_start()、 ob_get_contents()、 ob_clean()

$content = ob_get_contents();

file_put_contents(‘index.html’,$content);

6. True static typical cases

Static page

To determine whether a static page has been generated, if there is a static page, read the contents of the static page directly, if not, regenerate the static page.

Extension:

Give the generated static page a cache cycle, such as 5 minutes of cache. After 5 minutes, you need to regenerate the static page.

Determine whether it is in the cache cycle: the last modified time of the file + cache cycle > the current timestamp.
Filemtime - gets the modification time of the file and returns the timestamp.
    
if(file_exists($filename) && $filemtime($filename)+300>time()){
    include $filename;
    exit;
}
//Receive the passed ID
$id = $_GET['id'];
//Name the generated static page
$filename = 'news_info_id'.$id.'.html';

if(file_exists($filename)  && filemtime($filename)+300>time()){
    include $filename;
    exit;
}
//Open ob cache;
ob_start();
$pdo = new PDO('mysql:host=localhost;dbname=php69','root','root');
$pdo->exec('set names utf8');
$res = $pdo->query("select id,title,description from movie where id=$id");
$info = $res->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    < title > page title
    < meta name = "Keywords" content = "keyword list" / >
    < meta name = "description" content = "web page description" / >
    <link rel="stylesheet" type="text/css" href="" />
    <style type="text/css"></style>
    <script type="text/javascript"></script>
</head>
<body>
<?php echo $info['title']?>
<hr>
<?php echo $info['description']?>
</body>
</html>
<?php 
//Take out the contents in the OB cache and write them to a file;
$content = ob_get_contents();
file_put_contents($filename,$content);

7. True static advantages and disadvantages

Advantages: (1) fast speed, (2) high security, (3) favorable for SEO

Disadvantages: it occupies disk space. If it is too large, it will affect the disk response speed.

Under what circumstances is true static not recommended

(1) Page data updates frequently, it is best not to use true static (such as stock, fund, and other real-time quotation system)

(2) It will generate massive pages (such as BBS, CSDN)

(3) Query the page once, and do not query the page again

(4) Not willing to be crawled by search engines

(5) A page with a small number of visits

2、 Pseudo static

In the actual development project, our page is not suitable for the use of true static, but you not only want high page security, but also conducive to SEO, you can consider using pseudo static technology.

Pseudo static: disguise a dynamic address as a static address.

For example:

The original address is: http://www.abc.com/news.php?t…

Change to http://www.abc.com/news-sport…

1. Implementation mode

Using the rewrite mechanism of web server.

Rewrite mechanism: rewrite a request URL to another request!

In the rewrite mechanism, regular expressions are used to complete the rewriting rules.

For example:

index.html Rewritten as index.php

abc.php Rewrite to 123. PHP

news_ sport_ Id12.html rewritten as news.php?type=sport&id=12

Static page

2. How to open

Find the configuration file for the Apache server httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

Static page

3. Specific configuration

Three instructions:

Rewriteengine on enables the rewriting module,

Rewritecond override condition

Rewrite rules.

(1)、RewriteEngine on

Override engine switch, once turned on, all rewriting conditions are in effect.

(2) , rewritecond rewrite condition

When the conditions are met, complete the rewrite.

Grammar:

RewriteCond Judgment basis Conditional expression [Condition flag]


Judgment basisYou can use server variables. The server can get some specific information

Static page

Conditional expression, which can be in the form of regular or special identifier

-F indicates that it is a file,! – F is not a file

-D indicates that it is a directory,! – D is not a directory

Regular, regular expression string.

Condition flag:

[or] the or relationship between conditions. When multiple conditions appear, the default is and. If the condition should be or, it can be expressed by or!

[NC] conditions are not case sensitive. Case insensitive when matching conditions

[OR,NC]

(3) Rewrite rule definition

Define rewriting rules, which address should be rewritten to which destination address.

Grammar:

RewriteRule match address destination address [ID]

Matching address: the requested address. Regular matching can be used

Destination address: the address rewritten to, can use reverse reference! $n is the nth sub pattern matched by regular matching!

For example: RewriteRule goods ID (D +). HTML goods.php?id= $1

sign

[NC] case insensitive

[QSA] query string is appended. When the target address already has get parameters, the get parameters of the real request will be followed.


Case study: when visiting index.html Page, rewrite to index.php page

We use the distributed file configuration to complete the. Htaccess file

Step 1: open the configuration in the virtual host. The configuration supports distributed configuration.

Static page

Static page

Step 2: create a new. Htaccess file under the directory E / Amp / home (create the. Htaccess file by saving as in the editor)

Static page

Step 3: make specific configuration in the. Htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule index.html index.php
</IfModule>

Static page

Step 4: create a new one under the root directory of the website index.php File for testing

Static page

Static page

4. Introductory cases

If the file to be accessed exists, the file is accessed; if not, the file is rewritten

For example, we visit index.html File, if index.html If the file exists, the file is accessed, and if it does not exist, it is rewritten to the request one.php Page.

#Start rewriting engine
RewriteEngine on
#If the URL of the definition rewriting condition is not a file, the rewriting is performed
RewriteCond %{REQUEST_FILENAME} !-f [NC]
#Defining rewriting rules
RewriteRule index.html index.php [NC]

Static page

5. News cases are configured as pseudo static

news-info-id(d).html newsinfo.php?id=$1


$1 is a reverse reference, which is the content in the previous (\ d); $1 represents the person who (﹤ d) is, such as the following:;

news-info-id4.html newsinfo.php?id=4

news-info-id5.html newsinfo.php?id=5

. htaccess configuration:

#Start rewriting engine
RewriteEngine on
#If the URL of the definition rewriting condition is not a file, the rewriting is performed
RewriteCond %{REQUEST_FILENAME} !-f [NC]
#Defining rewriting rules
RewriteRule  news-info-id(\d).html   newsinfo.php?id=$1

Static page

effect:

Static page

6. Using rewrite mechanism to complete the anti-theft chain

What is anti-theft chain

Static page

How to prevent being stolen

First: using non-technical means, add watermark, add your own company logo

Second: use the rewrite mechanism to complete the anti-theft chain.


Core ideas:Determine the source of the request. When connecting to the image, you should judge whether the page of this website is connected. If it is, it is allowed to connect, if not, it is refused.

How to judge whether they visit the pictures themselves or other websites?

The header information of the referer records the source of the request.

Visit image resources on your website:

Static page

Other domain name websites visit image resources:

Static page


Idea: judge whether there is a domain name in the referer header. If so, you can access it by yourself. If not, you will be denied access.

Static page

#Turn on rewriting engine
RewriteEngine on 
#Defining rewriting conditions
RewriteCond %{HTTP_REFERER} !www.like520.cn
#Define rewriting rules - means to override to null [F] to prohibit access
RewriteRule \.(jpg|jpeg|gif|bmp)$ - [F]

Static page

The visit effect of other domain name websites is as follows:

Static page

Can give the website that steals chain a hint:

#Start rewriting engine
RewriteEngine on 
#Defining rewriting conditions
RewriteCond %{HTTP_REFERER} !www.like520.cn
#Define rewriting rules - means to override to null [F] to prohibit access
RewriteRule \.(gif|jpg|bmp)$ 110.gif

Static page

The visit effect of other domain name websites is as follows:

Static page

Recommended Today

Array of algorithms — sum of three numbers

Sum of three numbers difficultysecondary Here is an array of N integersnums, judgmentnumsAre there three elements a, B, C in a such that a + B + C = 0? Please find all triples that satisfy the condition and do not repeat.be careful:The answer cannot contain duplicate triples. Example:Given array nums = [- 1, 0, […]