• PHP页面静态化


    buffer    存储数据

    buffer 其实就是缓冲区,一个内存地址空间,主要用于存储数据区域。

    输出流程

    内容-->php buffer --> tcp --> 终端

    在PHP  ini 文件中有一个output_buffering = on

    获取缓冲区当中的数据   ob_get_contents();

        echo 1;
        echo '<br/>';
        echo ob_get_contents();

    如果在php.ini文件中output_buffering没有开启可以使用 ob_start()

    ob_start()  开启缓冲区

    PHP如何实现页面纯静态化

    基本方式:

    1、file_put_contents()函数:将一个字符串写入文件  返回写入到文件按内的字节数,失败返回FALSE

    2、PHP内置缓存机制实现页面静态化  - output_buffering

     

    如何触发系统生成纯静态化页面

    1、页面添加缓存时间

    2、手动触发方式

    3、crontab定时扫描程序

    页面添加缓存时间

    用户请求页面---->页面时间是否过期----->是  【动态页面并生成一份新的静态页面】

                      否   【获取静态页面】

    is_file('./index.html') && (time()-filemtime('./index.html') < 1200)

    如果我们的服务器存在这个文件,并且当前时间减去文件最后修改时间 小于 1200秒  认为缓存没有失效,直接加载静态文件。否则重新生成。

    filemtime()获取文件修改时间

    手动触发方式

    在后台开辟一个功能,针对首页做一个  开始更新,点击开始更新,走的程序是没有if判断最初的纯静态页面实现程序

    crontab定时扫描程序

    在Linux下,

    crontab -e 编辑crontab

    */5****php/data/static/index.php

    第一个*代表分

    第二个*代表时

    第三个*代表日

    第四个*代表月

    第五个*代表周

    */5****    */5代表每五分钟

    */5****php/data/static/index.php

    告诉Linux服务器每5分钟执行一次这个程序,生成纯静态页面

    **
     * 处理页面静态化业务逻辑
     * 有3种方案, 第一:定时(利用crontab来处理)  第二:人为触发 第三:在页面中控制时间来操作
    */
    //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');
        $s = ob_get_contents();
        file_put_contents('./index.html', $s);
        //ob_clean();
    }

    局部动态化

    实现步骤:编写接口-->ajax请求接口操作

    静态化页面中如果想加载动态的内容如何处理?

    ajax技术

    jquery中ajax请求方式

    $.ajax({
        url: '',  //请求服务器端的接口地址
        type: 'get',
        dataType: 'json',
        error: function(){
        },
        success: function(result){
        }
    })

    伪静态

    分析:通过正则表达式去分析伪静态URL地址

    例:http://static.com/newsList.php?type=2&category_id=1 =>  http://static.com/newsList.php/2/1.html

    其中: 2-->type=2,  1-->category_id=1

    $_SERVER:服务器当中的server变量  print_r($_SERVER)

    其中[PATH_INFO]的值 => /2/1.html

    preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。

    语法:

    int preg_match( string pattern, string subject [, array matches ] )
    pattern 正则表达式
    subject 需要匹配检索的对象
    matches 可选,存储匹配结果的数组, $matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推
    preg_match("/^/(d+)/(d+)(.html)$/", $_SERVER['PATH_INFO'], $pathInfo)

    d 匹配0-9当中的一个数字    d+  匹配一个或者多个     $pathInfo赋给这个变量

    接着进行匹配

    if(preg_match("/^/(d+)/(d+)(.html)$/", $_SERVER['PATH_INFO'], $pathInfo)) {
      //var_dump($pathInfo);
      $type = $pathInfo[1]; // 类型值
      $categoryId = $pathInfo[2]; // 所在栏目值
      // 引入数据库链接类
      require_once('./db.php'); 
      $sql = "select * from news where `category_id` = ".$categoryId." and `type` = ".$type." order by id desc";
      //  提取数据之后组装好放入模板中
      //...
    } else { // TODO }

    其中$_SERVER['PATH_INFO']

    /**
    * 利用PHP正则表达式来处理伪静态
    * 以http://static.com/newsList.php?type=2&category_id=1 =>  http://static.com/newsList.php/2/1.shtml
    */
    //echo 12;
    var_dump($_SERVER);
    
    if(isset($_SERVER['PATH_INFO'])) {
        // 解析 /2/1.shtml 匹配pathinfo值,如果没匹配到则数据不合法,若匹配到做相应处理
        if(preg_match("/^/(d+)/(d+)(.shtml)$/", $_SERVER['PATH_INFO'], $pathInfo)) {
            //var_dump($pathInfo);
            $type = $pathInfo[1]; // 类型值
            $categoryId = $pathInfo[2]; // 所在栏目值
            // 引入数据库链接类
            require_once('./db.php'); 
            $sql = "select * from news where `category_id` = ".$categoryId." and `type` = ".$type." order by id desc";
            try{
                $db = Db::getInstance()->connect();
                $result = mysql_query($sql, $db);
                $newsList = array();
                while($row = mysql_fetch_assoc($result)) {
                    $newsList[] = $row;
                }
                var_dump($newsList);
                exit;
            }catch(Exception $e) {
                // TODO
            }
        } else {
            // TODO
            die('url error!');
        }
    } else {
        // TODO
        // 错误提示,然后跳转到首页处理
        die('地址有误');
    }

    WEB服务器rewrite配置  达到伪静态的目的

    apache下rewrite配置

    1、虚拟域名配置

    2、httpd_vhosts.conf配置文件配置相关信息

    虚拟域名配置:

    C:workwamp64inapacheapache2.4.17conf   下  httpd.conf

    1、httpd.conf文件中开启相关模式

          LoadModule rewrite_module modules/mod_rewrite.so

          Include conf/extra/httpd-vhosts.conf

          vhosts文件增加相关域名   (ServerName)

    C:workwamp64inapacheapache2.4.17confextra   --->    httpd-vhosts.conf   --->ServerName

    2、httpd_vhosts.conf配置文件配置相关信息(虚拟域名的配置文件)

    DocumentRoot:  项目目录

    ServerName:虚拟域名

     继续配置hosts文件:C:WindowsSystem32driversetc  hosts

    伪静态配置案例:

    httpd-vhosts.conf  下配置

      RewriteEngine on    //开启

      RewriteRule^/detail/([0-9]*).html$ /detail.php?id=$1     //规则

      $1 是 ([0-9]*)

      例:state.com/detail/134.html    ==    state.com/detail.php?id=134

     #RewriteCond  两句话的意思  执行纯静态化内容

      -d 代表目录

      -f 代表文件

      意思:当我们服务器存在这个目录或者存在这个文件,这时候就让他去访问这个目录或者文件。。没有就会访问动态内容。

    nginx下rewrite配置

       rewrite ^/detail/(d+).html$ /detail.php?id=$1 last;

       rewrite     匹配规则       对应到的动态文件

    虚拟机或者服务器下   /etc/nginx/conf.d文件下

    static.singwa.com.conf文件

  • 相关阅读:
    2016 -03-08 静态库 .a
    2016 -03 -07 搜索功能 模糊查询
    2016 -03 -07 字符串是否包含字符串/字符
    2016-03-04 一个完整的model 样式
    2016-03-01 地图定位 以及失败
    2016-03-01 svn conerstone
    2016-03-01 svn .a 不能上传到conerstone上的解决
    2016-02-29 不能真机调试的问题
    2016-02-28 00:53:21 version 与build
    Add Two Numbers
  • 原文地址:https://www.cnblogs.com/alice-shan/p/9332902.html
Copyright © 2020-2023  润新知