search
HomeBackend DevelopmentPHP Tutorial用PHP往实现静态化

用PHP去实现静态化

      我们在PHP网站开发过程中为了网站的推广或者SEO的需要,需要对网站进行一定的静态化,这里设计到什么是静态页面,所谓的静态页面,并不是页面中没有动画等元素,而是指网页的代码都在页面中,即不需要再去执行PHP脚本等服务器端的语言去运行,我们可以直接访问到的网页,这就是静态网页。

     那么静态网页有什么好处呢?第一个主要原因就是因为搜索引擎,由于搜索引擎对PHP页面搜鹿和html页面的收录有一定的差别,并且面临着页面资源的占用问题,我们需要对.php文件进行静态化。有一种方式是改写访问地址,可以通过URL的PATHINFO模式来修改它,让它看上去更像一个静态页面,从而有更大的几率被搜索引擎抓取和收录。

     第二点原因就是它可以方便页面的加载,有时候我们去一些比如新浪、网易这些网站的首页,发现内容非常多,但是它的加载时间还真的不长,这里面也有静态化的功劳。网站可以在用户访问网站之前就通过一定的程序来进行静态化,生成静态页面,当用户去访问该页面的时候,由于访问的是静态页面,因此,访问速度会比访问动态页面的速度快了很多倍。这种技术对于大网站来说很有必要,对于小网站也可以采用。它在前台的表现是页面加载速度变快,在后台的表现是减少了数据库的连接,减少了数据库的压力,唯一的缺点就是相对占的硬盘多一些,不过,硬盘相对廉价的多。

      既然了解了静态化的一些优点,那么如何做到静态化呢?我们用PHP所能做到的静态化分为纯静态化和伪静态化,二者的却别在于圣经静态页面的机制不同,伪静态化就是通过解析URL和使用重写模式来运行动态页面,它只是对搜索引擎比较友好,并不是真正意义上的静态化,下面我们介绍一下纯静态化。

    所谓纯静态化,就是生成HTML文件的方式,我们需要开启PHP自带的缓存机制,即ob_start来开启缓存,并且在ob_start之前不能有任何输出,否则执行失败,然后我们用ob_get_contents函数来获取缓存中的内容,该函数会返回一个字符串,第三个函数就是ob_end_clean,它用来清空缓存中的内容并且关闭,成功返回True,失败返回False。

   下面请看实例:

    这里我们从数据库中取出数据并且把这些数据生成之后缓存到页面中,下面是该php文件:

<?php //开启缓存ob_start();//第一步连接数据库$conn = mysqli_connect("localhost","root","","bbs");//第二步设置相应的字符编码$setting = &#39;set names utf8&#39;;mysqli_query($conn,$setting);//第三步进行查询$sql = &#39;SELECT * FROM user&#39;;$result = mysqli_query($conn,$sql);//第四步把查询结果转化为一个数组$rows = mysqli_num_rows($result);$sqldata = array();for($i = 0;$i <$rows;$i ++){	$sqldata[] = mysqli_fetch_assoc($result);}//然后打印该信息var_dump($sqldata);//得到生成的html文件,下次访问就无需访问数据库了$msg = ob_get_contents();ob_end_clean();//把输出内容放入一个html文件中$f = fopen("static.html","w");fwrite($f,$msg);echo "静态化成功";

我们运行上述页面之后,发现该文件夹下自动多了一个html文件,下面是它的代码:

<pre class='brush:php;toolbar:false;'><b>array</b> <i>(size=6)</i>  0 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;0&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛星&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;bd04fcc97578ce33ca5fb331f42bc375&#39;</font> <i>(length=32)</i>  1 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;2&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小倩&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;61cb72858be523b9926ecc3d7da5d0c6&#39;</font> <i>(length=32)</i>  2 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;3&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小楠&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;a3d2de7675556553a5f08e4c88d2c228&#39;</font> <i>(length=32)</i>  3 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;4&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;刘强&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;fcdb06a72af0516502e5fdccc9181ee0&#39;</font> <i>(length=32)</i>  4 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;5&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;星哥&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;866a6cafcf74ab3c2612a85626f1c706&#39;</font> <i>(length=32)</i>  5 <font color=&#39;#888a85&#39;>=></font>     <b>array</b> <i>(size=4)</i>      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;6&#39;</font> <i>(length=1)</i>      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛勇&#39;</font> <i>(length=6)</i>      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;e93beb7663f3320eaa0157730d02dd0c&#39;</font> <i>(length=32)</i>

当然这份代码是我们写的php程序自动生成的,可以用浏览器直接访问,从而减轻了数据库的压力。


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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool