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

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 id="title">%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
聊聊如何利用伪静态来隐藏php后缀聊聊如何利用伪静态来隐藏php后缀Mar 20, 2023 pm 06:46 PM

伪静态是指将动态的url地址通过伪装成静态地址的方式进行访问的技术,而隐藏php后缀则是通过修改服务器配置,让访问动态页面时不再显示php后缀。这样做的好处是可以增强网站的安全性,避免被破解,同时也更加美观,增加用户体验。本文将详细介绍如何使用伪静态隐藏php后缀,以提升网站的安全性和用户体验。

优化网站SEO:伪静态隐藏php后缀实践优化网站SEO:伪静态隐藏php后缀实践Mar 07, 2024 pm 12:27 PM

众所周知,优化网站的SEO是网站运营中非常重要的一环。而众多网站使用的动态网页系统(如PHP)默认的URL带有拓展名(.php、.html等)会影响网站的SEO效果。为了提升网站的优化效果,一种常见的做法是将动态URL改为伪静态URL以隐藏拓展名,提高网站的用户体验和搜索引擎排名。本文将以“伪静态隐藏php后缀”为主题,介绍如何在PHP网站中实现这种优化,并

提升网站安全性:伪静态规则实现php后缀隐藏提升网站安全性:伪静态规则实现php后缀隐藏Mar 07, 2024 am 11:33 AM

提升网站安全性:伪静态规则实现PHP后缀隐藏随着互联网的发展,网站安全性问题日益凸显,其中包括对恶意攻击的防范和用户数据的保护。一种有效的措施是通过伪静态规则来隐藏PHP后缀,这有助于提高网站的安全性和保护用户隐私。在实现这一目标时,我们需要使用一些具体的代码示例来演示如何实现PHP后缀的隐藏。首先,我们需要了解什么是伪静态规则。伪静态是一种将动态网页链接转

利用ThinkPHP6实现伪静态利用ThinkPHP6实现伪静态Jun 20, 2023 pm 11:59 PM

随着互联网的快速发展,网站的建设越来越受到人们的关注。而众所周知,优化网站的SEO能够提高网站的排名及流量,并且伪静态就是网站SEO优化中不可或缺的一部分。在这篇文章中,我们将会利用ThinkPHP6实现伪静态,并且进一步探讨伪静态的优化和实现过程。什么是伪静态?在讲解伪静态实现之前,先来了解一下什么是伪静态。伪静态是一种通过对网页URL地址进行重写,使其看

PHP关闭伪静态代代码的方法详解PHP关闭伪静态代代码的方法详解Mar 24, 2024 pm 03:12 PM

PHP关闭伪静态代代码的方法详解随着网站开发的不断发展,伪静态代代码成为了优化网站链接、提升用户体验的重要一环。而有时候,我们也需要关闭伪静态代代码,可能是为了调试或者其他需求。在本文中,我们将逐步详细讨论如何关闭PHP中的伪静态代代码,并提供具体的代码示例。了解伪静态代代码首先,让我们简单了解一下什么是伪静态代代码。伪静态代代码是指通过URL重写技术,将动

深入了解伪静态:如何正确隐藏php后缀深入了解伪静态:如何正确隐藏php后缀Mar 08, 2024 am 10:15 AM

在当前网络技术日新月异的时代,网站的安全性和稳定性越来越受到人们的关注,其中隐藏网站的真实技术框架成为了许多站长们关心的焦点之一。伪静态技术,是一种常用的手段,可以有效提高网站的安全性,并且有助于防止站点被爬虫等恶意程序攻击。本文将深入探讨伪静态技术中如何正确隐藏php后缀,并提供具体代码示例。一、伪静态的概念伪静态,即伪静态化,是指在url地址中隐藏网站的

PHP关闭伪静态代代码的技巧分享PHP关闭伪静态代代码的技巧分享Mar 23, 2024 pm 03:12 PM

标题:PHP关闭伪静态代代码的技巧分享伪静态代代码是指一种让动态生成的URL看起来像静态页面链接的技术。在使用PHP开发网站时,有时候我们会遇到需要关闭伪静态代代码的情况,比如在调试阶段或者为了解决一些URL重定向问题。本文将分享一些关闭伪静态代代码的技巧,并提供具体的代码示例。一、关闭伪静态代代码的方法通过.htaccess文件关闭在网站根目录下找到.ht

PHP中关闭伪静态代代码的实用指南PHP中关闭伪静态代代码的实用指南Mar 23, 2024 pm 05:03 PM

PHP中关闭伪静态代代码的实用指南随着网站开发的不断发展,伪静态代代码(URLRewrite)在提高网站用户体验和搜索引擎优化方面起到了重要作用。然而,在某些情况下,关闭伪静态代代码也是必要的,比如在调试期间或者处理特定的异常情况。本文将详细介绍如何在PHP中关闭伪静态代代码,并提供具体的代码示例。什么是伪静态代代码?伪静态代代码是指通过修改URL或者重写

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version