为加速PHP程序而努力_PHP
动态网站的内容加速显示十分重要,本文通过对 PHP 几个函数的深入讨论,提出了 PHP 网页压缩和缓冲的解决方案
一、介绍几个控制 PHP 输出的函数
PHP4 采用了缓冲机制,在你决定发送以前,所有内容只是存在于缓冲中,而不是直接发送给浏览器,虽然你可以用 header 和 setcookie 函数来实现,但是这两个函数相比于功能强大的输出函数来说只是一点“雕虫小技”。让我们来看看这些函数的真本事:
void ob_start(void);
本函数告诉 PHP 处理器把所有输出重定向到内部缓冲,调用这个函数后,就不会有输出到浏览器。
string ob_get_contents(void);
本函数把输出缓冲返回到一个字符串,你可以用来把堆积起来的输出一起发送到浏览器。当然要先关掉缓冲。
int ob_get_length(void);
本函数返回输出缓冲的长度。
void ob_end_clean(void);
本函数清除并关闭缓冲。在输出到浏览器之前你需要使用这个函数。
void ob_implicit_flush ([int flag])
本函数用来控制隐式缓冲泻出,缺省为 off,如果打开时,对每个 print/echo 或者输出命令的结果都发送到浏览器。
二、采用输出控制来压缩 PHP 的输出
在开始之前,要保证你的 PHP4 编译时支持 Zlib。
首先,初始化输出缓冲:
ob_start();
ob_implicit_flush(0);
?>
然后产生所有的输出内容。
print("本例为压缩输出!");
?>
页面生成以后,采用:
$contents = ob_get_contents();
ob_end_clean();
?>
还要检查浏览器是否支持压缩数据,我们采用在变量 $HTTP_ACCEPT_ENCODING 中检查 "gzip, deflate"的办法:
if(ereg('gzip, deflate',$HTTP_ACCEPT_ENCODING)) {
// 产生 gzip 后的内容
} else {
echo $contents;
}
?>
下面我们分析怎样产生 gzip 输出:
// 告诉浏览器下面接收的是 gzip 数据。
header("Content-Encoding: gzip");
// 显示 gzip 文件的文件头
// 只要一次就够了
echo "x1fx8bx08x00x00x00x00x00";
// 计算长度和 CRC 校验码
$Size = strlen($contents);
$Crc = crc32($contents);
// 压缩数据
$contents = gzcompress($contents, 9);
// 不能直接在这里输出内容,因为还没有写入 CRC 呢!
$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 里能很容易的实现缓冲,我们来看例子:
// 对请求的 URI 产生唯一的文件名。
$cached_file=md5($REQUEST_URI);
if((!file_exists("/cache/$cached_file"))||(!is_valid("/cache/$cached_file"))) {
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 输出缓冲函数在操作脚本输出方面十分有用,把缓冲压缩后输出能减少 80% 的输出时间,这对于存取其他数据资源(例如数据库或者 XML)来说,也是一个很好的缓冲机制。

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





Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.
