搜索
首页php教程php手册php检测url页面是否使用gzip压缩小结

gzip压缩是一种很好的页面压缩传送方式,我们经常会在服务器端打开了,这样一个100K的文件通过gzip压缩之后可能只有20KB不到,下面我来介绍一下gzip压缩检测与开启方法.

如何打开gzip压缩传输呢?

apache,第1步,代码如下:

LoadModule deflate_module modules/mod_deflate.so

LoadModule headers_module modules/mod_headers.so

然后在http.conf加如下例代码:

# BEGIN GZIP  
#apache开启gzip压缩,前提要装gzip模块哦  
<ifmodule mod_deflate.c>  
AddOutputFilterByType DEFLATE text/text text/html 
text/plain text/xml text/css application/x-javascript 
application/javascript 
</ifmodule>  
# END GZIP

如果你是nginx服务器也可参考下面代码:

gzip on;  
gzip_min_length 1000;  
gzip_buffers 4 8k;  
gzip_http_version 1.1;  
gzip_types text/text text/html text/plain text/xml 
 text/css application/x-javascript application/javascript;

现在重启一下apache或nginx 即可,那么要如何检查是否正确开启gzip呢,我们可使用下面php代码:

//采用 gzip压缩网页  
//file_get_contents 直接获得的网页是乱码。  
header(&#39;Content-Type:text/html;charset=utf-8&#39; );  
$url = &#39;http://www.phprm.com&#39;;  
$file = fopen($url, "rb");    
//只读2字节  如果为(16进制)1f 8b (10进制)31 139则开启了gzip ;  
$bin = fread($file, 2);   
fclose($file);    
$strInfo = @unpack("C2chars", $bin);    
$typeCode = intval($strInfo[&#39;chars1&#39;].$strInfo[&#39;chars2&#39;]);    
$isGzip = 0;    
switch ($typeCode)    
{  
	case 31139:        
	//网站开启了gzip  
		$isGzip = 1;  
		break;  
	default:    
		$isGzip = 0;  
}    
$url = $isGzip ? "compress.zlib://".$url:$url; // 三元表达式  
$mierHtml = file_get_contents($url); //获得米尔军事网数据  
$mierHtml = iconv("gbk","utf-8",$mierHtml);  
echo $mierHtml;

例2代码如下:

<?php  
/*  
php 判断url页面是否使用gzip压缩  
*/ 
$ch = curl_init("http://www.phprm.com/");//url不能有转向  
curl_setopt($ch,CURLOPT_HTTPHEADER,array(&#39;Accept-Encoding: gzip, deflate&#39;));  
curl_setopt($ch, CURLOPT_HEADER, 1);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$buffer = curl_exec($ch);  
$curl_info = curl_getinfo($ch);  
curl_close($ch);  
$header_size = $curl_info["header_size"]; 
//头部信息长度  
$headers = substr($buffer, 0, $header_size); 
//获取头部信息  
$body = substr($buffer, $header_size); 
//获取网页内容  
   
function getEncoding(&$headers){  
	$arr=explode("rn",trim($headers));  
	array_shift($arr);  
	foreach($arr as $header){  
		list($k,$v)=explode(&#39;:&#39;,$header);  
		if (&#39;content-encoding&#39;==strtolower($k)){  
			return trim($v);  
		}  
	}  
	return false;  
}     
   
$encoding=getEncoding($headers);  
   
if ($encoding) {  
	echo "Using: ".$encoding;  
}else{  
	echo "None";  
}

                   

文章网址:

随意转载^^但请附上教程地址。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)