Home Backend Development PHP Tutorial 控制PHP的输出:缓存并压缩动态页面_php技巧

控制PHP的输出:缓存并压缩动态页面_php技巧

May 17, 2016 am 09:01 AM
cache

mod_gzip是一个Apache模块,其功能是使用Gzip压缩静态的html页面,遵循IETF标准的浏览器可以接受gzip编码(IE, Netscape等)。mod_gzip可以将页面的下载时间提高4-5倍。我强烈建议你在你的web服务器上使用mod_gzip。然而,我们还必须用PHP建立我们自己的压缩引擎。在这篇文章里,我将要介绍如何使用PHP的输出控制函数来大幅加速页面载入速度。

介绍PHP的输出控制函数

PHP4中最令人满意的事是——你可以让PHP缓存所有由脚本生成的输出,在你决定把它们送出之前,浏览器方是不会收到任何内容的。在脚本程序中你能用这些函数来设置header、cookies,然而这只是强大的输出函数的一小部分功能。

复制代码 代码如下:

void ob_start(void);
?>

告诉PHP处理器把所有的输出重定向到一个内部的缓存(buffer)中。在调用ob_start之前,没有输出会被送到浏览器去。

复制代码 代码如下:

string ob_get_contents(void);
?>

该函数将“输出缓存”(output buffer)以字符串的形式返回。你可以调用该函数把积累下来的输出送到浏览器中。(仅在把buffering功能关闭之后!!)

复制代码 代码如下:

int ob_get_length(void);
?>

返回缓存中的字符串的长度。

复制代码 代码如下:

void ob_end_clean(void);
?>

清空输出缓存,并将输出缓存关闭。在缓存中的内容输出到浏览器之前,必须使用这个函数。
void 501([int flag])
用来打开/关闭隐含的flush动作开关(默认是关)。如果flush是开的,每次调用print/echo或是其它输出命令的时候,输出的内容会被立即送到浏览器端。

使用输出控制来压缩PHP输出
你必须使用PHP4里编译的Zlib扩展包来压缩输出。如果需要的话,可以查看PHP文档中有关Zlib包的安装指导。
首先,初始化输出缓存:

复制代码 代码如下:

ob_start();
ob_implicit_flush(0);
?>

之后,用print, echo, 或其他你喜欢的方法生成所有输出内容,例如:

复制代码 代码如下:

print("Hey this is a compressed output!");
?>

页面生成后,我们取回输出内容:

复制代码 代码如下:

$contents = ob_get_contents();
ob_end_clean();
?>

之后,必须检测浏览器是否支持压缩数据。如果支持,浏览器会发给服务器端一个ACCEPT-ENCODEING HTTP头。我们只需检查$HTTP_ACCEPT_ENCODING变量中是否有“gzip,deflate”字串。

复制代码 代码如下:

if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) {
// 在这里生成 Gzip 压缩的内容
} else {
echo $contents;
}
?>

这种方法使用起来既简单又结构清晰。下面让我们看看如何生成压缩的输出:

复制代码 代码如下:

//告诉浏览器将要收到的是gzip数据
//当然在此之前,你已经检查了它们是否支持gzip,x-gzip数据格式
//如果支持的是x-gzip,那么下面的头就要用z-gzip来代替
header("Content-Encoding: gzip");

//显示gzip文件的头
//只需显示一次
echo "x1fx8bx08x00x00x00x00x00";

//计算出文件的大小和CRC码
$Size = strlen($contents);
$Crc = crc32($contents);

//压缩数据
$contents = gzcompress($contents, 9);

//我们不能就这样输出,因为CRC码是混乱的。
//如果我在这里使用“echo $contents”,压缩的数据会被送出,
//但是却不完整。文件最后的四个字节是CRC校验码,可是只发出去了三个字节。
//最后一个字节被丢掉了。我不知道这个bug在4.0.2版中解决了没有,
//不过最好避免错误的方法是把正确的CRC校验码加到压缩的数据的末尾。
//
//把旧的CRC校验码剥离
$contents = substr($contents, 0, strlen($contents) - 4);

//仅显示压缩的数据
echo $contents;

//输出CRC,和原来数据的大小(字节)
gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);

function gzip_PrintFourChars($Val) {
for ($i = 0; $i echo chr($Val % 256);
$Val = floor($Val / 256);
}
}

?>
//好了,你还可以按此方式附加上更多的压缩数据。

要想进行实际的测试,所有的脚本代码如下:

复制代码 代码如下:

ob_start();
ob_implicit_flush(0);

print("I'm compressed!n");

$contents = ob_get_contents();
ob_end_clean();

header("Content-Encoding: gzip");

echo "x1fx8bx08x00x00x00x00x00";

$Size = strlen($contents);
$Crc = crc32($contents);

$contents = gzcompress($contents, 9);

$contents = substr($contents, 0, strlen($contents) - 4);

echo $contents;

gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);

function gzip_PrintFourChars($Val) {
for ($i = 0; $i echo chr($Val % 256);
$Val = floor($Val / 256);
}
}
?>

缓存PHP输出

当PHP4还没问世,我不得不使用PHP3的时候,我对开发一些缓存机制来减少数据库的载入、对文件系统的存取十分感兴趣。在PHP3中没有什么特别好的方法,但是有了输出缓存之后,在PHP4中一切变得容易多了。
这有一个简单的例子:

复制代码 代码如下:

//为请求的URI构造一个文件名
$cached_file=md5($REQUEST_URI);

if((!file_exists("/cache/$cached_file"))||(!is_valid("/cache/$cached_file"))) {
//is_valid函数验证缓存,你可以用这个函数检查Cache是否过期或其他特定的条件。
//如果文件不在Cache中或者不可用则生成输出
ob_start();
ob_implicit_flush(0);
//在此输出……

$contents = ob_get_contents();
ob_end_clean();
$fil=fopen($cached_file,"w+");
fwrite($fil,$contents,$strlen($contents));
fclose($fil);
}

/如果请求的文件在缓存中且可用,则:
readfile($cached_file);

?>

这是一个简单的例子,使用输出缓存,你可以建立一个复杂的内容生成系统,对不同的块或程序使用不同的缓存机制,等等……

结论

PHP输出控制函数对把脚本生成的输出重定向到缓存中十分有用。为支持gzip的浏览器输出压过的缓存数据可以减少载入时间。也可作为缓存机制来减少对数据源的存取(数据库或文件),这对使用XML意义重大。
如果我们用PHP建立一个引擎,缓存从数据源得到的数据(xml文档和数据库),并且动态的生成XML格式的内容(没有外观-presentation)我们可以得到这些XML的输出,并使用XSLT转换成任意一种我们想要的外观格式(html, wap, palm, pdf等)。使用PHP4的输出缓存和Sablotron XSLT扩展可以很好地完成这个任务。

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

Video Face Swap

Video Face Swap

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

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)

Where are video files stored in browser cache? Where are video files stored in browser cache? Feb 19, 2024 pm 05:09 PM

Which folder does the browser cache the video in? When we use the Internet browser every day, we often watch various online videos, such as watching music videos on YouTube or watching movies on Netflix. These videos will be cached by the browser during the loading process so that they can be loaded quickly when played again in the future. So the question is, in which folder are these cached videos actually stored? Different browsers store cached video folders in different locations. Below we will introduce several common browsers and their

How to view and refresh dns cache in Linux How to view and refresh dns cache in Linux Mar 07, 2024 am 08:43 AM

DNS (DomainNameSystem) is a system used on the Internet to convert domain names into corresponding IP addresses. In Linux systems, DNS caching is a mechanism that stores the mapping relationship between domain names and IP addresses locally, which can increase the speed of domain name resolution and reduce the burden on the DNS server. DNS caching allows the system to quickly retrieve the IP address when subsequently accessing the same domain name without having to issue a query request to the DNS server each time, thereby improving network performance and efficiency. This article will discuss with you how to view and refresh the DNS cache on Linux, as well as related details and sample code. Importance of DNS Caching In Linux systems, DNS caching plays a key role. its existence

Speed ​​up your applications: A simple guide to Guava caching Speed ​​up your applications: A simple guide to Guava caching Jan 31, 2024 pm 09:11 PM

A Beginner's Guide to Guava Cache: Speed ​​Up Your Applications Guava Cache is a high-performance in-memory caching library that can significantly improve application performance. It provides a variety of caching strategies, including LRU (least recently used), LFU (least recently used), and TTL (time to live). 1. Install Guava cache and add the dependency of Guava cache library to your project. com.goog

Will HTML files be cached? Will HTML files be cached? Feb 19, 2024 pm 01:51 PM

Title: Caching mechanism and code examples of HTML files Introduction: When writing web pages, we often encounter browser cache problems. This article will introduce the caching mechanism of HTML files in detail and provide some specific code examples to help readers better understand and apply this mechanism. 1. Browser caching principle In the browser, whenever a web page is accessed, the browser will first check whether there is a copy of the web page in the cache. If there is, the web page content is obtained directly from the cache. This is the basic principle of browser caching. Benefits of browser caching mechanism

The relationship between CPU, memory and cache is explained in detail! The relationship between CPU, memory and cache is explained in detail! Mar 07, 2024 am 08:30 AM

There is a close interaction between the CPU (central processing unit), memory (random access memory), and cache, which together form a critical component of a computer system. The coordination between them ensures the normal operation and efficient performance of the computer. As the brain of the computer, the CPU is responsible for executing various instructions and data processing; the memory is used to temporarily store data and programs, providing fast read and write access speeds; and the cache plays a buffering role, speeding up data access speed and improving The computer's CPU is the core component of the computer and is responsible for executing various instructions, arithmetic operations, and logical operations. It is called the "brain" of the computer and plays an important role in processing data and performing tasks. Memory is an important storage device in a computer.

Advanced Usage of PHP APCu: Unlocking the Hidden Power Advanced Usage of PHP APCu: Unlocking the Hidden Power Mar 01, 2024 pm 09:10 PM

PHPAPCu (replacement of php cache) is an opcode cache and data cache module that accelerates PHP applications. Understanding its advanced features is crucial to utilizing its full potential. 1. Batch operation: APCu provides a batch operation method that can process a large number of key-value pairs at the same time. This is useful for large-scale cache clearing or updates. //Get cache keys in batches $values=apcu_fetch(["key1","key2","key3"]); //Clear cache keys in batches apcu_delete(["key1","key2","key3"]);2 .Set cache expiration time: APCu allows you to set an expiration time for cache items so that they automatically expire after a specified time.

Spring Boot performance optimization tips: create applications as fast as the wind Spring Boot performance optimization tips: create applications as fast as the wind Feb 25, 2024 pm 01:01 PM

SpringBoot is a popular Java framework known for its ease of use and rapid development. However, as the complexity of the application increases, performance issues can become a bottleneck. In order to help you create a springBoot application as fast as the wind, this article will share some practical performance optimization tips. Optimize startup time Application startup time is one of the key factors of user experience. SpringBoot provides several ways to optimize startup time, such as using caching, reducing log output, and optimizing classpath scanning. You can do this by setting spring.main.lazy-initialization in the application.properties file

How to save video files from browser cache to local How to save video files from browser cache to local Feb 23, 2024 pm 06:45 PM

How to Export Browser Cache Videos With the rapid development of the Internet, videos have become an indispensable part of people's daily lives. When browsing the web, we often encounter video content that we want to save or share, but sometimes we cannot find the source of the video files because they may only exist in the browser's cache. So, how do you export videos from your browser cache? This article will introduce you to several common methods. First, we need to clarify a concept, namely browser cache. The browser cache is used by the browser to improve user experience.

See all articles