Table of Contents
PHP页面静态化:真静态的两种方案,php静态
%title%
php静态化页面方法,伪静态的也可以
php对于页面静态化问题
Home php教程 php手册 PHP页面静态化:真静态的两种方案,php静态

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

Jun 13, 2016 am 09:28 AM
Pseudo-static

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>
Copy after login

@黑眼诗人

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

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

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 就可以了
    只是包含文件的时候注意 你分离出去的文件只有

    中的部分
    再就是看好页面样式
    好的程序员 会将很多部分分离
    这样便于统一管理
     
  • Statement of this Website
    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

    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

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Let's talk about how to use pseudo-static to hide the php suffix Let's talk about how to use pseudo-static to hide the php suffix Mar 20, 2023 pm 06:46 PM

    Pseudo-static refers to the technology of accessing dynamic URL addresses by disguising them as static addresses, while hiding the PHP suffix is ​​to modify the server configuration so that the PHP suffix is ​​no longer displayed when accessing dynamic pages. The advantage of this is that it can enhance the security of the website and avoid being cracked, while also making it more beautiful and increasing the user experience. This article will introduce in detail how to use pseudo-static to hide the php suffix to improve the security and user experience of the website.

    Optimizing website SEO: practice of pseudo-static hiding php suffix Optimizing website SEO: practice of pseudo-static hiding php suffix Mar 07, 2024 pm 12:27 PM

    As we all know, optimizing the SEO of a website is a very important part of website operation. The default URLs of dynamic web systems (such as PHP) used by many websites have extensions (.php, .html, etc.), which will affect the SEO effect of the website. In order to improve the optimization effect of the website, a common practice is to change the dynamic URL to a pseudo-static URL to hide the extension name and improve the user experience and search engine ranking of the website. This article will take "pseudo-static hidden php suffix" as the theme, introduce how to achieve this optimization in PHP websites, and

    Improve website security: Pseudo-static rules implement PHP suffix hiding Improve website security: Pseudo-static rules implement PHP suffix hiding Mar 07, 2024 am 11:33 AM

    Improve website security: Pseudo-static rules implement PHP suffix hiding. With the development of the Internet, website security issues have become increasingly prominent, including the prevention of malicious attacks and the protection of user data. An effective measure is to hide the PHP suffix through pseudo-static rules, which helps improve the security of the website and protect user privacy. In achieving this goal, we need to use some specific code examples to demonstrate how to achieve PHP suffix hiding. First, we need to understand what pseudo-static rules are. Pseudo-static is a method of converting dynamic web page links into

    Using ThinkPHP6 to achieve pseudo-static Using ThinkPHP6 to achieve pseudo-static Jun 20, 2023 pm 11:59 PM

    With the rapid development of the Internet, website construction has attracted more and more attention. As we all know, optimizing the SEO of a website can improve the ranking and traffic of the website, and pseudo-static is an integral part of the SEO optimization of the website. In this article, we will use ThinkPHP6 to implement pseudo-static and further explore the optimization and implementation process of pseudo-static. What is Pseudo-Static state? Before explaining pseudo-static implementation, let’s first understand what pseudo-static is. Pseudo-static is a method of rewriting the URL address of a web page to make it appear

    From principle to practice: detailed explanation of pseudo-static hidden php suffix From principle to practice: detailed explanation of pseudo-static hidden php suffix Mar 07, 2024 pm 03:27 PM

    Title: From Principle to Practice: Detailed explanation of pseudo-static hidden PHP suffix In network development, in order to improve the security of the website and enhance the user experience, hiding the file extension in the URL has become a common operation. Among them, hiding the PHP file suffix is ​​a commonly used technical means, which can improve the security of the website, increase the aesthetics of the website, and is also beneficial to search engine optimization. This article will explain in detail the principle and practical operation of pseudo-static hiding PHP suffix, and provide specific code examples. 1. The principle of pseudo-static hiding PHP suffix pseudo-static

    Detailed explanation of how to turn off pseudo-static code in PHP Detailed explanation of how to turn off pseudo-static code in PHP Mar 24, 2024 pm 03:12 PM

    Detailed explanation of how to turn off pseudo-static code in PHP With the continuous development of website development, pseudo-static code has become an important part of optimizing website links and improving user experience. Sometimes, we also need to turn off pseudo-static code, perhaps for debugging or other needs. In this article, we will discuss in step-by-step detail how to turn off pseudo-static code in PHP and provide concrete code examples. Understanding pseudo-static code First, let us briefly understand what pseudo-static code is. Pseudo-static code refers to the use of URL rewriting technology to convert dynamic

    Tips for turning off pseudo-static code in PHP Tips for turning off pseudo-static code in PHP Mar 23, 2024 pm 03:12 PM

    Title: Tips for turning off pseudo-static code in PHP Pseudo-static code refers to a technique that makes dynamically generated URLs look like static page links. When developing websites using PHP, sometimes we encounter situations where we need to turn off pseudo-static code, such as during the debugging phase or to solve some URL redirection problems. This article will share some tips for turning off pseudo-static code and provide specific code examples. 1. The method to turn off the pseudo-static code is to turn off the .htaccess file and find .ht in the root directory of the website.

    A closer look at pseudo-static: how to properly hide php suffixes A closer look at pseudo-static: how to properly hide php suffixes Mar 08, 2024 am 10:15 AM

    In the current era of rapid changes in network technology, the security and stability of websites have attracted more and more attention. Among them, hiding the real technical framework of the website has become one of the focuses of many webmasters. Pseudo-static technology is a commonly used method that can effectively improve the security of a website and help prevent the site from being attacked by malicious programs such as crawlers. This article will delve into how to correctly hide the php suffix in pseudo-static technology and provide specific code examples. 1. The concept of pseudo-static. Pseudo-static, that is, pseudo-static, refers to hiding the website in the URL address.

    See all articles