Home >php教程 >php手册 >php页面静态化笔记

php页面静态化笔记

WBOY
WBOYOriginal
2016-06-06 09:52:58982browse
<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * php页面静态化
 * 静态化分为 真静态和伪静态
 * 静态化的有点在于 SEO 和 安全性 使用缓存机制还可以大大提高网站的速度
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">/*</span><span style="color: #008000;">*
 * php自带的缓存机制
 * 需要在php.ini中进行配置
 * 或者使用ob_start()-> output_buffering:on
 * 关于几个缓存的函数:ob_start();ob_clean();ob_get_content();ob_end_clean();关闭缓存同时清空
 * ob_flush();输出缓存里面的内容并且清空,但是不关闭 ob_end_flush();关闭同时输出
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">/**/</span>
<span style="color: #008080;">ob_start</span>();<span style="color: #008000;">//</span><span style="color: #008000;">开启缓存</span>
<span style="color: #0000ff;">echo</span> 'yyy';<span style="color: #008000;">//</span><span style="color: #008000;">没有开启缓存的情况下会报错,因为没有遇到header的时候,php默认发送一个header,这个header无法修改</span>
<span style="color: #008080;">header</span>('content-type:text/htm;charset=utf-8'<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> '123'<span style="color: #000000;">;
</span><span style="color: #008080;">ob_get_contents</span>();<span style="color: #008000;">//</span><span style="color: #008000;">得到缓存的内容</span>
<span style="color: #008080;">ob_end_clean</span><span style="color: #000000;">();
</span><span style="color: #0000ff;">echo</span> 'aa';<span style="color: #008000;">//</span><span style="color: #008000;">放在程序缓存里面</span>
<span style="color: #008080;">header</span>('content-type:text/htm;charset=utf-8');<span style="color: #008000;">//</span><span style="color: #008000;">运行的时候会报错,因为缓存已经关闭</span>


<span style="color: #008000;">/*</span><span style="color: #008000;">
 * 利用php自带的缓存机制做真缓存
 * 真缓存的页面的内容应该是长期都不需要变的,并且不同用户看到的东西都是一样的
 * 原理:用户第一次访问的时候查询数据库,然后得到缓存区的内容,并且放到缓存文件里面
 * </span><span style="color: #008000;">*/</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">*
 </span><span style="color: #008000;">*/</span>
<span style="color: #008080;">ob_start</span><span style="color: #000000;">();
</span><span style="color: #008000;">//</span><span style="color: #008000;">判断缓存文件是否存在</span>
<span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$html_filename</span>) &&  (<span style="color: #008080;">filemtime</span>(<span style="color: #800080;">$html_filename</span>)+30) > <span style="color: #008080;">time</span>())<span style="color: #008000;">//</span><span style="color: #008000;">超过30秒则重新生成</span>
<span style="color: #000000;">{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">直接取出缓存页面的内容</span>
    <span style="color: #0000ff;">echo</span> <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$html_filename</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">;
    
} 
</span><span style="color: #0000ff;">echo</span> 'Your content'<span style="color: #000000;">;
</span><span style="color: #800080;">$html_content</span>=<span style="color: #008080;">ob_get_contents</span><span style="color: #000000;">();
</span><span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">;
</span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$html_filename</span>,<span style="color: #800080;">$html_content</span><span style="color: #000000;">);

</span><span style="color: #008000;">/*</span><span style="color: #008000;">
 *  * 上述原理的缺陷:1.实时性不够  2.不利于seo优化
 * 解决办法:在数据库发生增加和更新的时候更新缓存文件
 * </span><span style="color: #008000;">*/</span>

<span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_REQUEST</span>['act'] == 'add')<span style="color: #008000;">//</span><span style="color: #008000;">update 也是</span>
<span style="color: #000000;">{
  </span><span style="color: #008000;">//</span><span style="color: #008000;">先获取模版文件</span>
  <span style="color: #800080;">$template_content</span>=<span style="color: #008080;">file_get_contents</span>('template.tpl'<span style="color: #000000;">);
  </span><span style="color: #008000;">//</span><span style="color: #008000;">替换标签</span>
  <span style="color: #800080;">$php_content</span>=<span style="color: #008080;">str_replace</span>("{",'<?php echo ',<span style="color: #800080;">$template_content<span style="color: #000000;">);
  </span><span style="color: #800080;">$php_content</span>=<span style="color: #008080;">str_replace</span>("}",'?>',<span style="color: #800080;">$template_content</span><span style="color: #000000;">);
  </span><span style="color: #008000;">//</span><span style="color: #008000;">生成缓存文件</span>
  <span style="color: #800080;">$php_tplname</span>='php_tpl.php'<span style="color: #000000;">;
  </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$php_tplname</span>,<span style="color: #800080;">$php_content</span><span style="color: #000000;">);
  </span><span style="color: #0000ff;">include</span>(<span style="color: #008080;">file_put_contents</span><span style="color: #000000;">);
  </span><span style="color: #800080;">$html_content</span>=<span style="color: #008080;">ob_get_contents</span><span style="color: #000000;">();
  </span><span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">;
  </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$html_filename</span>,<span style="color: #800080;">$html_content</span><span style="color: #000000;">);
  
}

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 真静态的缺陷:生成的 缓存文件很多,占硬盘
 * 对于海量的html生成,可以考虑用伪静态
 </span><span style="color: #008000;">*/</span>

<span style="color: #800080;">$str</span>='123,33,333'<span style="color: #000000;">;
</span><span style="color: #008080;">preg_match</span>('/\d\d\d\d/is',<span style="color: #800080;">$str</span>,<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">匹配连续四个出现的数字 返回到arr 只是匹配一个 arr[0]</span>
<span style="color: #008080;">preg_match</span>('/(\d)(\d)/is',<span style="color: #800080;">$str</span>,<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">这时候arr返回三个 0放置匹配的字符串,arr[n]按照表达式顺序返回匹配的字符串

//对于需要全部匹配的使用preg_match_all()函数;正则表达式里面需要'' 不要使用""</span>

<span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 使用apache的rewrite机制
 * 注意点:apache的配置 开启rewrite module 也要设置derectory的权限 allowoverride from all 
 * 文件 .htacss //匹配规则是从上而下的,可以设置error页面
 * RewireEngine On
 * news-id(\d)\.html$  news.php?id=$1
 * news-id(\d).html$    error.php      #错误页面放到最后
 </span><span style="color: #008000;">*/</span>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn