zoukankan      html  css  js  c++  java
  • PHP生成静态页

    t1.php

    <?php
    // 方法一根据模版生成静态页面
    // replaceTemplateString函数用于替换模板中指定字符串
    function replaceTemplateString($templateString) {
        // 用来替换的变量
        $title = "文章标题";
        $body = "这里是文章主体";
        // 替换模板中指定字符串
        $showString = str_replace ( "%title%", $title, $templateString );
        $showString = str_replace ( "%body%", $body, $showString );
        // 返回替换后的结果
        return $showString;
    }
    
    
    $template_file = "template.html";
    $new_file      = "new.html";
    // 模版文件指针
    $template_juBing = fopen ( $template_file, "r" );
    // 要生成的文件指针
    $newFile_juBing = fopen ( $new_file, "w" );
    
    // 方式一获取整体模板内容字符串,替换后赋给新文件
    $templateString = fread ( $template_juBing, filesize ( $template_file ) );
    $showString = replaceTemplateString ( $templateString ); // 替换模板中字符串
    fwrite ( $newFile_juBing, $showString ); // 将替换后的内容写入生成的HTML文件
    
    /*
    // 方式二循环读取模版每行内容字符串,替换后依次添加到新文件
    while ( ! feof ( $template_juBing ) ) { // feof() 函数检测是否已到达文件末尾。如果文件指针到了末尾或者出错时则返回 TRUE。否则返回FALSE(包括 socket 超时和其它情况)。
        $templateString = fgets ( $template_juBing ); // fgets(file,length) 从文件指针中读取一行并返回长度最多为 length - 1 字节长度的字符串,包括换行符。如果没有指定 length,则默认为 1K,或者说 1024 字节。
        $showString = replaceTemplateString ( $templateString );
        fwrite ( $newFile_juBing, $showString ); // 第一次往打开的指针文件中写入内容时会替换指针文件中原有内容,在该文件指针关闭前,fwrite函数再添加内容会在已添加内容之后
    }
    */
    // 关闭文件指针
    fclose ( $newFile_juBing );
    fclose ( $template_juBing );
    
    
    
    /*
    数据库与静态页的关系
    通常数据库内添加一条信息同后,生成一个该信息的静态页面,所以最好在数据库表中添加一字段存储对应静态页面的路径文件名,方便以后的修改,删除
    
    模版的替换
    一般来说,如果需要修改静态HTML页面的模版,通常的做法是将所有的已经生成的HTML页面删除,然后重新创建新的HTML页面。(或者说全部重新覆盖生成)
    
    静态页上的动态操作
    有些时候,在创建的静态HTML页上面也需要进行一些动态操作。例如,新闻系统中的每篇新闻要统计点击率。
    可通过一个宽和高都为0像素的图像控件来隐藏的调用一个php页面来实现页面计数器功能,如
    <img width='0' height='0' src='counter.php?fileid=S001'>
    
    链接目录的静态页
    通常对于使用静态页面的系统来说,往往将连接列表的目录页也生成静态HTML文件供访问者浏览
    注意的是因为每增加或者减少一条数据库信息都会对链接列表产生影响,因此,每次对数据库信息进行添加和删除时都需要更新链接目录的静态页。
    分页的设计可以通过创建多个链接目录的静态页来完成。
    */
    
    
    // 方法二根据缓冲区生成
    ob_start (); // 当缓冲区激活时,并且有ob_end_clean()的情况下,所有输出打印的非文件头信息均不会输出打印到页面,而是保存在内部缓冲区。如果没有ob_end_clean(),则信息既被存在内部缓冲区,也被输出打印
    ?>
    this is test Output Control
    <?php
    echo "<br>this is test Output Control<br>";
    include_once 'cache/newFile.php';
    
    $contents = ob_get_contents (); // 获取缓冲区到此为止存储的信息,缓冲区只保存会向页面浏览器输出打印的内容,php执行代码等不会保存
    // $contents = ob_get_clean(); // 获取缓冲区到此为止存储的信息,并关闭清除缓冲区
                                    
    // ob_end_flush();//输出打印缓冲区到此为止存储的信息,并关闭清除缓冲区
    
    ob_end_clean (); // 关闭清除缓冲区的内容
    
    file_put_contents ( $new_file, $contents );// 向文件写入内容
    
    
    //===============决定什么时候更新缓存:
    /*
    根据程序的需要,缓存可以采取多种形式。最常见的3种方式是:
    
    时间触发缓存(过期的时间戳)
    内容改变触发缓存(发现数据改变后,相应地更新缓存)
    人工触发缓存(人工的方式告知系统信息超期并且强制产生新的缓存)
    
    缓存需求可能是以上原理的一个或多个的综合。这里事例时间触发方式。然而,在一个全面的缓存机制中,3种方式的综合将被使用。
    */
    //假设有 "new.html" 静态页,第一行包含时间戳的字符串为 <!-- Cache Created at: 1368855939 -->
    $new_file      = "new.html";
    $check_begin_str = "<!-- Cache Created at: ";
    $check_end_str = " -->";
    
    $template_juBing = fopen ( $new_file, "r" );
    $templateString = fgets ( $template_juBing ); // 获取第一行数据
    
    $templateString=str_replace($check_begin_str,"",$templateString);
    $templateString=str_replace($check_end_str,"",$templateString);
    $templateString=str_replace(" ","",$templateString); // 获取第一行最终时间戳
    if((time()-$templateString)<60) {
        //当前时间戳减去页面生成时间戳,小于指定的60,则不生成
    }
    else {
        //重新生成静态页
    }
    ?>

     

    template.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>%title%</title>
    </head>
    <body>
    <H1>%title%</H1>
    <hr>
    <pre>%body%</pre>
    </body>
    </html>
  • 相关阅读:
    作业6
    作业8
    作业7
    作业5
    作业4
    作业3
    作业2
    作业1
    浏览器跨域的细节
    解析node-cors模块
  • 原文地址:https://www.cnblogs.com/dreamhome/p/3036687.html
Copyright © 2011-2022 走看看