Php动态页面缓存内容编码错误

时间:2016-10-07 20:30:47

标签: php html cache-control

我正在尝试在PHP中创建页面缓存。 首先,当我添加此行ob_start('ob_gzhandler')以打开输出缓冲时,我的Firefox浏览器将显示此错误

  

内容编码错误

     

您要查看的页面无法显示   因为它使用无效或不受支持的压缩形式。请   联系网站所有者告知他们这个问题。

在Chrome上我遇到此错误

  

无法访问此网站

     

http://example.conm/index的网页   可能暂时停止或可能已永久移动到新的   网址。 ERR_CONTENT_DECODING_FAILED

这是我在使用之前使用的代码,但我不知道出了什么问题。

    <?php
$dynamiccatch = true;
    //Catch method Enable this function in Define.php
    if($dynamiccatch == true && (!isset($_SESSION['username'])) && (!isset($_SESSION['cjladmin']))){ 
    $cache_ext  = '.html'; //file extension
    $cache_time     = 3600;  //Cache file expires after these seconds (1 hour = 3600 sec)
    $cache_folder   = 'cache/'; //folder to store Cache files
    $ignore_pages   = array('', '');
    $dynamic_url    = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; // requested dynamic page (full url)
    $cache_file     = $cache_folder.md5($dynamic_url).$cache_ext; // construct a cache file
    $ignore = (in_array($dynamic_url,$ignore_pages))?true:false; //check if url is in ignore list

    if (!$ignore && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)){ //check Cache exist and it's not expired.
        ob_start('ob_gzhandler'); //Turn on output buffering, "ob_gzhandler" for the compressed page with gzip.
        readfile($cache_file); //read Cache file
        echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cache_file)).', Page : '.$dynamic_url.' -->';
        ob_end_flush(); //Flush and turn off output buffering
        exit(); //no need to proceed further, exit the flow.
    }
    //Turn on output buffering with gzip compression.
    ob_start('ob_gzhandler');
    ######## End catch technology #########
    }
    ?>
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title><?php echo 'Index | '.WebName;?></title>
        <?php include('meta.php');?>
      </head>
      <body>
    My website here
    </body>
    </html>
    <?php 
    if($dynamiccatch == true && (!isset($_SESSION['username'])) && (!isset($_SESSION['cjladmin']))){
    ######## Catch footer ends here #########
    if (!is_dir($cache_folder)) { //create a new folder if we need to
        mkdir($cache_folder);
    }if(!$ignore){
        $fp = fopen($cache_file, 'w');  //open file for writing
        fwrite($fp, ob_get_contents()); //write contents of the output buffer in Cache file
        fclose($fp); //Close file pointer
    }ob_end_flush(); //Flush and turn off output buffering
    }
    ?>

1 个答案:

答案 0 :(得分:1)

导致页面出现问题的原因是UTF-8 BOM

  • UTF-8 BOM'ed文件字符串将以以下字节开头。 EF BB BF 个字符。{/ 1>

您应该将网页的内容从UTF-8 BOM更改为UTF-8。这应该可以解决你的问题。

有很少或更少的IDE可以在不让你知道的情况下发生。

示例情况:

-> File Edited 
-> File Saved (UTF-8)
-> File Uploaded (File encoding changed to UTF-8 BOM)

如果发生这种情况,请将IDE的Enconding从默认设置为'UTF-8 Without BOM'。

正如您在评论中所描述的那样,您正在使用Notepad ++

我只建议使用Notepad ++来预览文件而不是开发,如果你想要Notepad ++用于开发,那么你应该像下面那样正确配置它:

Goto `Encoding`  and select the `Encoding in UTF-8 without BOM` option.

什么是BOM?

  

字节顺序标记(BOM)由数据流开头的字符代码U + FEFF组成,其中它可以用作定义字节顺序和编码形式的签名,主要是未标记的明文文件。在某些更高级别的协议下,在该协议中定义的Unicode数据流中可能必须(或禁止)使用BOM。

BOM在哪里有用?

  

BOM在文本类型的开头是有用的,但是不知道它们是大端还是小端格式 - 它也可以作为指示文件是Unicode的提示,与传统编码相反,它还充当所使用的特定编码形式的签名。

相关问题