Home >php教程 >php手册 >PHP页面静态化:真静态的两种方案,php静态

PHP页面静态化:真静态的两种方案,php静态

WBOY
WBOYOriginal
2016-06-13 09:28:091027browse

PHP页面静态化:真静态的两种方案,php静态

----------------------------------------------------------------------------------------------

<span>方案1:如果静态文件存在,且生成时间30秒内,直接返回静态页面(有时间延迟)<br />/*<br />|------------------<br />| <www.chenwei.ws><br />|------------------<br />*/<br /></span><span>header</span>('content-type:text/html;charset=utf-8'<span>);</span>
<span>$id</span> = <span>$_GET</span>['id'] ? <span>intval</span>(<span>$_GET</span>['id']) : ''<span>;
</span><span>if</span>(<span>$id</span> === '') <span>die</span>('请输入要查询的新闻id!'<span>);
</span><span>$html_file</span> = "news-id-".<span>$id</span>.".html"<span>;

</span><span>//1.</span><span>主要代码</span>
<span>if</span>(<span>file_exists</span>(<span>$html_file</span>) && <span>filemtime</span>(<span>$html_file</span>) + 30 >= <span>time</span><span>())
{
    </span><span>echo</span> '静态页面:'<span>;
    </span><span>echo</span> <span>file_get_contents</span>(<span>$html_file</span>);<span>exit</span><span>;
}

</span><span>//</span><span>这里也可以使用DB工具类</span>
<span>$con</span> = <span>mysql_connect</span>('localhost', 'root', '123456'<span>);
</span><span>if</span>(!<span>$con</span><span>)
{
    </span><span>die</span>('连接失败!'<span>);
}
</span><span>mysql_select_db</span>('testdb', <span>$con</span><span>);
</span><span>$sql</span> = "select * from bo_question where question_id = <span>$id</span>"<span>;
</span><span>$res</span> = <span>mysql_query</span>(<span>$sql</span>, <span>$con</span><span>);
</span><span>if</span>(<span>$row</span> = <span>mysql_fetch_assoc</span>(<span>$res</span><span>))
{
    </span><span>ob_start</span>();<span>//2.</span><span>启动ob缓存</span>
    <span>header</span>('content-type:text/html;charset=utf-8'<span>);
    </span><span>echo</span> '<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /></head>'<span>;
    </span><span>echo</span> '<table>;
    </span><span>echo</span> '<tr><td>问题详细内容</td></tr>'<span>;
    </span><span>echo</span> "<tr><td>标题:{<span>$row</span>['question_title']}</td></tr>"<span>;
    </span><span>echo</span> "<tr><td>详细:{<span>$row</span>['question_detail']}</td></tr>"<span>;
    </span><span>echo</span> '</table>'<span>;
    </span><span>$ob_str</span> = <span>ob_get_contents</span><span>();
    </span><span>//3.</span><span>把ob_str保存到一个静态文件页面,取文件名有讲究:1.唯一标识该新闻 2.利于seo</span>
    <span>file_put_contents</span>("news-id-".<span>$id</span>.".html", <span>$ob_str</span><span>);<br />  <br />  //关闭数据库连接(非必须; 非长连接下,脚本执行完会自动关闭) <br />  mysql_close($con);
}</span><span>else</span><span>{
    </span><span>echo</span> '没有查询到资源!'<span>;
}</span>

 @黑眼诗人


方案2:使用模板替换技术(没有时间延迟)<br />/*<br />|------------------<br />| <www.chenwei.ws><br />|------------------<br />*/
<span>$oper</span> = <span>$_POST</span>['oper'<span>];//添加操作
</span><span>if</span>(<span>$oper</span> === 'add'<span>)
{
    </span><span>$title</span> = <span>$_POST</span>['title'<span>];
    </span><span>$content</span> = <span>$_POST</span>['content'<span>];
    
    </span><span>//</span><span>如果严格按MVC,这里应该调用model了</span>
    <span>$con</span> = <span>mysql_connect</span>('localhost', 'root', '123456'<span>);
    </span><span>if</span>(!<span>$con</span><span>)
    {
        </span><span>die</span>('连接失败!'<span>);
    }
    </span><span>mysql_select_db</span>('news', <span>$con</span><span>);
    </span><span>$sql</span> = "insert into question(null, '<span>$title</span>', '<span>$content</span>', '')"<span>;
    </span><span>if</span>(<span>mysql_query</span>(<span>$sql</span>, <span>$con</span><span>))
    {
        </span><span>//1.</span><span>生成静态文件 </span>
        <span>$id</span> = <span>mysql_insert_id</span><span>();
        </span><span>$html_filename</span> = 'news-id'.<span>$id</span>.'.html'<span>;
        </span><span>$html_fp</span> = <span>fopen</span>(<span>$html_filename</span>, 'w'<span>);
        
        </span><span>//2.</span><span>把模板文件读取(news.html)</span>
        <span>$fp</span> = <span>fopen</span>('news.tpl', 'r'<span>);
        </span><span>//</span><span>r 只读方式打开; r+ 读写方式打开; w 写入方式打开:文件内容将被清空!如果文件不存在将创建; a 以追加的方式打开
        
        //3.循环读取
        //如果没有读到文件的最后,就一直读取</span>
        <span>while</span>(!<span>feof</span>(<span>$fp</span><span>))
        {
            </span><span>//</span><span>一行行读</span>
            <span>$row</span> = <span>fgets</span>(<span>$fp</span><span>);
            </span><span>//</span><span>把占位符替换掉 => 可以自定义完整的替换规则函数</span>
            <span>$row</span> = <span>str_replace</span>('%title%', <span>$title</span>, <span>$row</span>);<span>//</span><span>如果不重新赋值$row, $row值不会改变</span>
            <span>$row</span> = <span>str_replace</span>('%content%', <span>$content</span>, <span>$row</span><span>);
            
            </span><span>fwrite</span>(<span>$html_fp</span>, <span>$row</span><span>);//4.将内容写入静态文件
        }<br />
        </span><span>//5.</span><span>文件必须关闭</span>
        <span>fclose</span>(<span>$html_fp</span><span>);
        </span><span>fclose</span>(<span>$fp</span><span>);
        
        </span><span>echo</span> "添加成功。<a href='newslist.php'>点击查看新闻!</a>"<span>;
    }
    </span><span>else</span><span>
    {
        </span><span>die</span>('添加失败!'<span>);
    }
}
</span><span>//</span><span>此时在新闻列表内,点击查看详情的链接,可以改成生成的静态页面地址,直接进入静态文件。

//news.tpl模板文件</span><span>
/*</span><span>
<html>
    <head>
        <meta charset="utf-8" />
        <title>%title%</title>
    </head>
    <body>
        <h1>%title%</h1>
        <pre class="brush:php;toolbar:false">%content%
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