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

WBOY
Freigeben: 2016-07-13 10:22:01
Original
914 Leute haben es durchsucht

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>
Nach dem Login kopieren

@黑眼诗人

Nach dem Login kopieren
方案2:使用模板替换技术(没有时间延迟)<br />/*<br />|------------------<br />| <www.chenwei.ws><br />|------------------<br />*/
Nach dem Login kopieren
<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%
Nach dem Login kopieren
*/

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

php静态化页面方法,伪静态的也可以

这里是jetee.cn中静态化会员列表部分,根据这部分代码,可以理解静态化的一些概念。

静态化函数部分
/**
* @get list member item 获得静态化模板中要替换变量的内容。
* @param str: To replace the string
* @return string
*/
function get_staticize_replace_str()
{
$replace_str=""; //replace string
$query="select member_id,email_name from member";
$result=Mysql::query($query);
while($row=Mysql::fetch_assoc($result))
{
$replace_str.="

  • ".$row["member_id"]."";
    $replace_str.="
  • ".$row["email_name"]."
    ";
    }
    return $replace_str;
    }

    /**
    * @替换静态化模板中的变量生成静态化页面。
    * @
    * @return void
    */
    function staticize_list_member()
    {
    $replace_str=$this->get_staticize_replace_str();
    $templet=fopen(TENDAO_DIR."/templets/default/list_member.html","r");
    $new_file=fopen(TENDAO_DIR."/member/list_member.html","w");
    while(!feof($templet))
    {
    $ripe=fgets($templet);
    $ripe=str_replace("{member_items}",$replace_str,$ripe);
    fwrite($new_file,$ripe);
    }

    if (file_exists(TENDAO_DIR."/member/list_member.html")) {
    Msg("静态化会员列表成功!返回主页中……",TENDAO_ROOT,0,3000);
    exit();
    } else {
    Msg("静态化会员列表没有成功!返回主页中……",TENDAO_ROOT,0,3000);
    }

    fclose($templet);
    fclose($new_file);
    }

    /**
    * @静态化模板
    * @
    ......余下全文>>
     

    php对于页面静态化问题

    方法很多啊
    在php中 用include_once 就可以了
    只是包含文件的时候注意 你分离出去的文件只有

    中的部分
    再就是看好页面样式
    好的程序员 会将很多部分分离
    这样便于统一管理
     

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/851760.htmlTechArticlePHP页面静态化:真静态的两种方案,php静态 ---------------------------------------------------------------------------------------------- 方案1:如果静态文件存...
  • Verwandte Etiketten:
    Quelle:php.cn
    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Beliebte Tutorials
    Mehr>
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage