Home >php教程 >PHP源码 >php 开启gzip加速网站

php 开启gzip加速网站

PHP中文网
PHP中文网Original
2016-05-25 17:10:311053browse

php代码

<?php
/**
* @author Seraphim
* @copyright 2012
*/
// 
function sendheader($last_modified, $p_type, $content_length = 0)
{
  // 设置客户端缓存有效时间
  header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
  header("Cache-Control: max-age=315360000");
  header("Pragma: ");
  // 设置最后修改时间
  header("Last-Modified: " . $last_modified);
  // 设置文件类型信息
  header($p_type);
  header("Content-Length: " . $content_length);
}
define(&#39;ABSPATH&#39;, dirname(__file__) . &#39;/&#39;);
$cache = true;
$cachedir = &#39;cache/&#39;; //存放gz文件的目录,确保可写
if (empty($_SERVER[&#39;QUERY_STRING&#39;]))
  exit();
$gzip = strstr($_SERVER[&#39;HTTP_ACCEPT_ENCODING&#39;], &#39;gzip&#39;);
if (empty($gzip))
  $cache = false;
$key = array_shift(explode(&#39;?&#39;, $_SERVER[&#39;QUERY_STRING&#39;]));
$key = str_replace(&#39;../&#39;, &#39;&#39;, $key);
$filename = ABSPATH . $key;
$symbol = &#39;_&#39;;
$rel_path = str_replace(ABSPATH, &#39;&#39;, dirname($filename));
$namespace = str_replace(&#39;/&#39;, $symbol, $rel_path);
$cache_filename = ABSPATH . $cachedir . $namespace . $symbol . basename($filename) .
  &#39;.gz&#39;; //生成gz文件路径
$ext = array_pop(explode(&#39;.&#39;, $filename)); //根据后缀判断文件类型信息
$type = "Content-type: text/html"; //默认的文件类型
switch ($ext)
{
  case &#39;css&#39;:
    $type = "Content-type: text/css";
    break;
  case &#39;js&#39;:
    $type = "Content-type: text/javascript";
    break;
  case &#39;gif&#39;:
    $cache = false;
    $type = "Content-type: image/gif";
    break;
  case &#39;jpg&#39;:
    $cache = false;
    $type = "Content-type: image/jpeg";
    break;
  case &#39;png&#39;:
    $cache = false;
    $type = "Content-type: image/png";
    break;
  default:
    exit();
}
if ($cache)
{
  if (file_exists($cache_filename))
  { // 假如存在gz文件
    $mtime = filemtime($cache_filename);
    $gmt_mtime = gmdate(&#39;D, d M Y H:i:s&#39;, $mtime) . &#39; GMT&#39;;
    if ((isset($_SERVER[&#39;HTTP_IF_MODIFIED_SINCE&#39;]) && array_shift(explode(&#39;;&#39;, $_SERVER[&#39;HTTP_IF_MODIFIED_SINCE&#39;])) ==
      $gmt_mtime))
    {
      // 与浏览器cache中的文件修改日期一致,返回304
      header("HTTP/1.1 304 Not Modified");
      // 发送客户端header
      header("Content-Encoding :gzip");
      sendheader($gmt_mtime, $type);
    }
    else
    {
      // 读取gz文件输出
      $content = file_get_contents($cache_filename);
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding: gzip");
      // 发送数据
      echo $content;
    }
  }
  else
    if (file_exists($filename))
    { // 没有对应的gz文件
      $mtime = mktime();
      $gmt_mtime = gmdate(&#39;D, d M Y H:i:s&#39;, $mtime) . &#39; GMT&#39;;
      // 读取文件
      $content = file_get_contents($filename);
      // 去掉空白的部分
      // $content = ltrim($content);
      // 压缩文件内容
      $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding: gzip");
      // 发送数据
      echo $content;
      // 写入文件
      file_put_contents($cache_filename, $content);
    }
    else
    {
      header("HTTP/1.0 404 Not Found");
    }
}
else
{ // 处理不使用Gzip模式下的输出。原理基本同上
  if (file_exists($filename))
  {
    $mtime = filemtime($filename);
    $gmt_mtime = gmdate(&#39;D, d M Y H:i:s&#39;, $mtime) . &#39; GMT&#39;;
    if ((isset($_SERVER[&#39;HTTP_IF_MODIFIED_SINCE&#39;]) && array_shift(explode(&#39;;&#39;, $_SERVER[&#39;HTTP_IF_MODIFIED_SINCE&#39;])) ==
      $gmt_mtime))
    {
      // 与浏览器cache中的文件修改日期一致,返回304
      header("HTTP/1.1 304 Not Modified");
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding :gzip");
    }
    else
    {
      // 读取文件输出
      $content = file_get_contents($filename);
      // 发送客户端header
      sendheader($gmt_mtime, $type, strlen($content));
      // 发送数据
      echo $content;
    }
  }
  else
  {
    header("HTTP/1.0 404 Not Found");
  }
}
?>

Statement:
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