


概念
PHP静态化分为:纯静态化 和 伪静态化;
纯静态化又分为:局部静态化 和 完全静态化
纯静态化:是把PHP生成的动态页面保存成静态的html文件,用户访问该静态页面,而不是用户每一次访问都重新生成一张相同的网页,优点就是减小服务器开销,
局部静态化:是生成的静态文件中,有局部的数据还是通过ajax技术动态获取的;
完全静态化:即不存在动态获取数据的情况,所以内容都来自静态的html页面
伪静态化:Apache服务器rewrite配置
纯静态化的实现
利用php内置的ob函数实现页面的静态化,大概步骤如下:
<?php ob_start();//开启缓存 ?> <p>我是要生成的静态内容,也可以在该处链接数据库生成动态内容于此</p> <?php file_put_contents( 'index.html', ob_get_clean() );//把生成的静态内容保存到index.html文件,而不是输出到浏览器 ?>
触发系统生成纯静态化页面
方法:页面添加缓存时间;手动触发
页面添加缓存时间
<?php $file_name = 'index.html'; if(file_exists( $file_name ) && filemtime( $file_name ) - time() < 10 ){//如果文件是存在并且最后修改时间小于设定时间 10s //filemtime( $file_name );//得到文件最后修改时间 //time();//当前时间 require_once( $file_name );//引入文件 }else{ ob_start( ); ?> <p>我是要生成的静态内容</p> <?php file_put_contents( $file_name, ob_get_contents() )//输出到浏览器 }
如果后台数据存在更细,定时刷新不能及时更改静态页面,怎么办?所有引入了手动触发的功能
Linux下的crontab定时扫描程序
*/5****php/data/static/index.php
PHP伪静态
Apache服务器rewrite配置
在httpd.conf文件中,找到
#注释:去掉前边的" # "开启rewrite服务,重启服务器生效
#LoadModule rewrite_module modules/mod_rewrite.so
#注释:http-vhosts.conf文件是虚拟域名配置的文件,开启改文件可以配置虚拟域名,一般默认是开启的
#Include conf/extra/httpd-vhosts.conf
rewrite伪静态配置
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "c:/Apache24/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error.log" CustomLog "logs/dummy-host.example.com-access.log" common #配置规则如下所示 RewriteEngine on RewriteRule ^/vidio/([0-9]*).html$ /vidio.php?id=$1 </VirtualHost>
也可用.htacess文件,放在网站目录下,无需重启服务器。
更多php相关知识,请访问php教程!
The above is the detailed content of PHP realizes page staticization, pure staticization and pseudo-staticization. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools