网页压缩传输文档
网页压缩传输文档 

在编译php时,你只要加上 --with-zlib 就可以通过下面的函数压缩传输网页,对于较大的网页 
效果比较明显 :) 

文件一 (此文件为压缩函数) 
gzdoc.php 
<? 
ob_start
();  
ob_implicit_flush(0);  
function 
GetHeader() 
{  
 
$headers getallheaders();  
 while (list(
$header$value) = each($headers)) 
  {  
 
$Message .= "$header: $value<br>\n";  
  }  
 return 
$Message;  
}  
function 
CheckCanGzip() 
{  
 global 
$HTTP_ACCEPT_ENCODING$PHP_SELF$Wget$REMOTE_ADDR$S_UserName;  
 if (
connection_timeout() || connection_aborted()){  
 return 
0;  
}  
if ((
strpos('catoc'.$HTTP_ACCEPT_ENCODING'gzip')) || $Wget == 'Y'
{  
 if (
strpos('catoc'.$HTTP_ACCEPT_ENCODING'x-gzip')) 
   {  
   
$ENCODING "x-gzip";  
   
$Error_Msg str_replace('<br>','',GetHeader());  
   
$Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n";  
   
$Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n";  
   
//mail('[email protected]', "User have x-gzip output in file $PHP_SELF!!!", $Error_Msg);  
   
}else 
   {  
   
$ENCODING "gzip";  
   }  
  return 
$ENCODING;  
  } 
  else 
  {  
  return 
0;  
  }  
}  
function 
GzDocOut() 
{  
 global 
$PHP_SELF$CatocGz$REMOTE_ADDR$S_UserName;  
 
$ENCODING CheckCanGzip();  
 if (
$ENCODING
 {  
 print 
"\n<!-- Use compress $ENCODING -->\n";  
 
$Contents ob_get_contents();  
 
ob_end_clean();  
 if (
$CatocGz == 'Y'
  {  
  print 
"Not compress lenth: ".strlen($Contents)."<BR>";  
  print 
"Compressed lenth: ".strlen(gzcompress($Contents))."<BR>";  
  exit;  
  }else 
  {  
  
header("Content-Encoding: $ENCODING");  
  }  
 print 
pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00);  
 
$Size strlen($Contents);  
 
$Crc crc32($Contents);  
 
$Contents gzcompress($Contents);  
 
$Contents substr($Contents0strlen($Contents) - 4);  
 print 
$Contents;  
 print 
pack('V',$Crc);  
 print 
pack('V',$Size);  
 exit;  
 }else 
 {  
 
ob_end_flush();  
 
$Error_Msg str_replace('<br>','',GetHeader());  
 
$Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n";  
 
$Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n";  
 
//mail('[email protected]', "User can not use gzip output in file $PHP_SELF!!!", $Error_Msg);  
 
exit;  
 }  
}  
?> 

文件二 (此文件为范列) 
<?  
  
include("gzdoc.php"); 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> 这是一个测试 </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
</HEAD> 
<BODY BGCOLOR="#FFFFFF"> 
OK 啦!!!!呵呵。。。 
</BODY> 
</HTML> 
<? 
  gzdocout
(); 
?> 

二 、把使用频繁的变量存入内存中 
你在编译php时加上选项  --enable-sysvsem --enable-sysvshm 就可以实现了 
下面是文件 
文件一(函数文件) 
mem_class.php 
<? 
class SharedMem
    var 
$SemaphoreID
    var 
$SharedMemID
    var 
$VarIndexes
    function 
SharedMem(){ 
        
$this->SemaphoreID sem_get(1000,1,0600); 
        
$this->SharedMemID shm_attach(2000,65535,0600); 
        
sem_acquire($this->SemaphoreID); 
        
$this->VarIndexes = @shm_get_var($this->SharedMemID,0); 
        if(empty(
$this->VarIndexes)){ 
            
$this->VarIndexes = array(); 
            
shm_put_var($this->SharedMemID,0,$this->VarIndexes); 
        } 
        
sem_release($this->SemaphoreID); 
    } 

    
//取出存在共享内存中的某变量,返回false表示共享内存中无该变量 
    
function read($name){ 
        if(!isset(
$this->VarIndexes[$name])) return false
        
$GLOBALS[$name] = shm_get_var($this->SharedMemID,$this->VarIndexes[$name]); 
        return 
true
    } 

    
//将指定变量存入共享内存 
    
function save($name){ 
        if(!isset(
$this->VarIndexes[$name])){ 
            
$this->VarIndexes[$name] = count($this->VarIndexes)+1
            
shm_put_var($this->SharedMemID,0,$this->VarIndexes); 
        } 
        
shm_put_var($this->SharedMemID,$this->VarIndexes[$name],$GLOBALS[$name]); 
    } 

?> 

文件二 (exsample) 
mem_test.php 
<?  
include("mem_class.php"); 
$test = new SharedMem(); 
$abc '123'
$test->save('abc'); 
unset(
$abc); 
$test->read('abc'); 
print 
$abc
?>