用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 = 'set names utf8';mysqli_query($conn,$setting);//第三步进行查询$sql = 'SELECT * FROM user';$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='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'0'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛星'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'bd04fcc97578ce33ca5fb331f42bc375'</font> <i>(length=32)</i> 1 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小倩'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'61cb72858be523b9926ecc3d7da5d0c6'</font> <i>(length=32)</i> 2 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小楠'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'a3d2de7675556553a5f08e4c88d2c228'</font> <i>(length=32)</i> 3 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'4'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'刘强'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'fcdb06a72af0516502e5fdccc9181ee0'</font> <i>(length=32)</i> 4 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'5'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'星哥'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'866a6cafcf74ab3c2612a85626f1c706'</font> <i>(length=32)</i> 5 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'6'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛勇'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'e93beb7663f3320eaa0157730d02dd0c'</font> <i>(length=32)</i>
当然这份代码是我们写的php程序自动生成的,可以用浏览器直接访问,从而减轻了数据库的压力。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Pygame's Font text and font Pygame uses the pygame.font module to create a font object to achieve the purpose of drawing text. Commonly used methods of this module are as follows: Name Description pygame.font.init() Initialize the font module pygame.font.quit() Uninitialize the font module pygame.font.get_init() Check whether the font module has been initialized and return a Boolean value . pygame.font.get_default_font() gets the file name of the default font. Returns the file name of the font in the system pygame.font.get_fonts() gets all

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers
