Home  >  Article  >  Backend Development  >  PHP page staticization: two truly static solutions, php static_PHP tutorial

PHP page staticization: two truly static solutions, php static_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:01916browse

PHP page staticization: two truly static solutions, php static

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

<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>

@黑eyedpoet


方案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%
*/

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

PHP static page method, pseudo-static can also be used

Here is the static member list part of jetee.cn. Based on this part of the code, you can understand some concepts of staticization.

Static function part
/**
* @get list member item Get the content of the variable to be replaced in the static template.
* @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.="25edfb22a4f469ecb59f1190150159c6".$row["member_id"]."bed06894275b65c1ab86501b08a632eb";
$replace_str.="25edfb22a4f469ecb59f1190150159c6".$row["email_name"] ."bed06894275b65c1ab86501b08a632eb0c6dc11e160d3b678d68754cc175188a";
}
return $replace_str;
}

/**
* @Replace variable generation in static templates Static page.
* @
* @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("Static member list successful! Return to homepage...", TENDAO_ROOT,0,3000);
exit();
} else {
Msg("Staticizing the member list failed! Return to the homepage...",TENDAO_ROOT,0,3000);
}

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

/**
* @static template
* @
...The rest of the text>>

php has issues with page staticization

There are many ways
Just use include_once in php
Just when including files, please note that the files you separate are only the parts in 6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956
Then watch carefully Page style
Good programmers will separate many parts
so that unified management

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851760.htmlTechArticlePHP page staticization: two truly static solutions, php static ---------- -------------------------------------------------- ----------------------------------Option 1: If static files are stored...
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