Maison  >  Article  >  php教程  >  [PHP]PHP页面静态化:真静态的两种方案

[PHP]PHP页面静态化:真静态的两种方案

WBOY
WBOYoriginal
2016-06-06 19:49:39878parcourir

---------------------------------------------------------------------------------------------- /* |------------------ | 方案1:如果静态文件存在,且生成时间30秒内,直接返回静态页面(有时间延迟) www.farwish.com |------------------ */ header

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

<span>/*<br>|------------------<br>| <span>方案1:如果静态文件存在,且生成时间30秒内,直接返回静态页面(有时间延迟)</span> <www.farwish.com><br>|------------------<br>*/<br></www.farwish.com></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> '<meta http-equiv="content-type" content="text/html;charset=utf-8">'<span>;
    </span><span>echo</span> '
'; echo ''; echo ""; echo ""; echo '
问题详细内容
标题:{$row['question_title']}
详细:{$row['question_detail']}
'; $ob_str = ob_get_contents(); //3.把ob_str保存到一个静态文件页面,取文件名有讲究:1.唯一标识该新闻 2.利于seo file_put_contents("news-id-".$id.".html", $ob_str);
  
  //关闭数据库连接(非必须; 非长连接下,脚本执行完会自动关闭)
  mysql_close($con); }
else{ echo '没有查询到资源!'; }

 

[PHP]PHP页面静态化:真静态的两种方案


/*<br>|----------------------------------------------------<br>| 方案2:使用模板替换技术(没有时间延迟) <www.farwish.com><br>|----------------------------------------------------<br>*/</www.farwish.com>
<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>

    
        <meta charset="utf-8">
        <title>%title%</title>
    
    
        <h1>%title%</h1>
        <pre class="brush:php;toolbar:false">%content%
*/

 

Link: http://www.cnblogs.com/farwish/p/3874264.html

@黑眼诗人 

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn