PHP uses zlib extension to implement GZIP compressed output
This article mainly introduces the method of PHP using zlib extension to achieve GZIP compression output. Now I share it with everyone. Friends in need can refer to it.
The example of this article describes the method of PHP using zlib extension to implement GZIP compression output. Methods. Share it with everyone for your reference, the details are as follows:
Generally, when we have a large amount of data transmission and hope to reduce the bandwidth pressure on the server, we will adopt a method to compress the file transmission. Using zlib in PHP can also implement gzip Compressed output, let’s look at a summary of various methods of GZIP compressed output.
GZIP (GNU-ZIP) is a compression technology. After GZIP compression, the page size can be reduced to 30% or even smaller than the original size. In this way, users will feel refreshed and happy when browsing!
Preparation
1. Can’t find the php_zlib.dll file?
Zlib compression has been built into php since php4.3, so at least in Windows environment there is no need to install zlib.
2. Install and build the php running environment
Since it is not enough to enable gzip configuration through the php.ini configuration file to achieve php gzip compression output, it requires the support of apache, so it is recommended to install and build php apache mysql operating environment.
php gzip configuration steps
1. Open the php.ini configuration file and find zlib.output_compression = Off, Change
zlib.output_compression = Off ;zlib.output_compression_level = -1
to
zlib.output_compression = On zlib.output_compression_level = 6
Example 1
PHP uses zlib extension to implement page GZIP compression output
Code
function ob_gzip($content) // $content 就是要压缩的页面内容 { if(!headers_sent() && extension_loaded("zlib") && strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip"))//判断页面头部信息是否输出,PHP中zlib扩 展是否已经加载,浏览器是否支持GZIP技术 { $content = gzencode($content." n//此页已压缩",9); //为准备压缩的内容贴上"//此页已压缩"的注释标签,然后用zlib提供的gzencode()函数执行级别为9的压缩,这个参数值范围是0-9,0 表示无压缩,9表示最大压缩,当然压缩程度越高越费CPU。 //用header()函数给浏览器发送一些头部信息,告诉浏览器这个页面已经用GZIP压缩过了! header("Content-Encoding: gzip"); header("Vary: Accept-Encoding"); header("Content-Length: ".strlen($content)); } return $content; //返回压缩的内容
After the function is written, call it with ob_start, so the original ob_start()
becomes
Copy code The code is as follows:
ob_start('ob_gzip'); //给ob_start()加一个参数,参数名就是刚才的函数名。这样当内容进入缓冲区后PHP就会调用ob_gzip函数把它压缩了。
Final end buffer
##Copy code The code is as follows:
ob_end_flush(); //结束缓冲区,输出内容。当然,不用这个函数也行,因为程序执行到最后会自动将缓冲区内容输出。
<?php //调用一个函数名为ob_gzip的内容进行压缩 ob_start('ob_gzip'); //输出内容 ob_end_flush(); //这是ob_gzip函数 function ob_gzip($content) { if(!headers_sent()&&extension_loaded("zlib") &&strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip")) { $content = gzencode($content." n//此页已压缩",9); header("Content-Encoding: gzip"); header("Vary: Accept-Encoding"); header("Content-Length: ".strlen($content)); } return $content; } ?>
Example 2
zlib compression and decompression swf file codeExample of file://没有加入判断swf文件是否已经压缩,入需要可以根据文件的第一个字节是'F'或者'C'来判断 压缩swf文件: //-------------------------------------------------------------------------------------------------- //文件名 $filename = "test.swf"; //打开文件 $rs = fopen($filename,"r"); //读取文件的数据 $str = fread($rs,filesize($filename)); //设置swf头文件 $head = substr($str,1,8); $head = "C".$head; //获取swf文件内容 $body = substr($str,8); //压缩文件内容,使用最高压缩级别9 $body = gzcompress($body, 9); //合并文件头和内容 $str = $head.$body; //关闭读取的文件流 fclose($rs); //创建一个新的文件 $ws = fopen("create.swf","w"); //写文件 fwrite($ws,$str); //关闭文件留 fclose($ws); //---------------------------------------------------------------------------------------------------- ?>
//---------------------------------------------------------------------------------------------------- //文件名 $filename = "test.swf"; //打开文件 $rs = fopen($filename,"r"); //读取文件的数据 $str = fread($rs,filesize($filename)); //设置swf头文件 $head = substr($str,1,8); $head = "F".$head; //获取swf文件内容 $body = substr($str,8); //解压缩文件内容 $body = gzuncompress($body); //合并文件头和内容 $str = $head.$body; //关闭读取的文件流 fclose($rs); //创建一个新的文件 $ws = fopen("create.swf","w"); //写文件 fwrite($ws,$str); //关闭文件留 fclose($ws); //---------------------------------------------------------------------------------------------------- ?>
Example 3
Enable php zlib (gzip) compressed outputphp gzip configuration knowledge points:
1. By default, PHP does not enable zlib whole-site compression output, but uses theob_gzhandler function on pages that require compressed output. You can only choose one of the two. , otherwise an error will be reported.
zlib.output_compressionThe default value is Off, you can set it to On, or output buffer size (default is 4k)
3,zlib.output_compression_level represents the compression ratio. The default recommended compression ratio is 6. The optional range is 1-9. -1 represents turning off php zlib (gzip) compression
2. Save the php.ini configuration file , and restart the apache server3. Open the apache configuration file httpd.conf, configure and loaddeflate_module
This step is the most critical step to enable php gzip compression output configuration. Many netizens will say that even though I have enabled the php gzip configuration in the php.ini configuration file, I still don’t realize php gzip compression. This is because apache is not loaded withdeflate_module. The method is as follows, change
#LoadModule deflate_module modules/mod_deflate.so
PHP uses Nginx to implement reverse proxy
PHP uses Redis under Windows system
The above is the detailed content of PHP uses zlib extension to implement GZIP compressed output. For more information, please follow other related articles on the PHP Chinese website!

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











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 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 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.

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

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

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 is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.
