【问题标题】:Php Dynamic page cache Content Encoding Errorphp动态页面缓存内容编码错误
【发布时间】:2016-10-07 20:30:46
【问题描述】:

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

内容编码错误

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

在 chrome 上我得到这个错误

无法访问此网站

网页http://example.conm/index 可能暂时关闭,或者它可能已永久移动到新的 网址。 ERR_CONTENT_DECODEDING_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
    }
    ?>

【问题讨论】:

  • 请修正您的代码格式
  • @VuralAcar 这是我的问题,我已经尝试让它穿上超过 24 小时,但仍然不知道该怎么做
  • 我确定是utf-8 file 内部有UTF-8 BOM 的问题
  • @VuralAcar 我认为是真的,我删除了代码顶部的所有内容,所以让我弄清楚它来自哪里
  • 你在使用 Notepad++ 吗?

标签: php html cache-control


【解决方案1】:

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

  • UTF-8 BOM 文件字符串将以下列字节开头。 EF BB BF 字符。

您应该将页面的编码从UTF-8 BOM 更改为UTF-8。这应该可以解决您的问题。

很少或更少的 IDE 会在不让您知道的情况下导致它发生。

示例情况:

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

如果发生这种情况,请将 IDE 的编码从默认设置为“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 的提示,与传统编码相反,此外,它充当所使用的特定编码形式的签名。

【讨论】:

  • 我在记事本++中使用 UTF-8
  • 但 Notepad++ 将其更改为 UTF-8 BOM,这就是我问你是否使用 Notepad++ 的原因;)
  • 哪个更适合开发,一旦我得到一个更好的,我如何将它更改为 UTF-8?
  • 我可以推荐使用 PHPStorm 进行 PHP 开发,或者使用 Netbeans。两者都是全栈 Web 开发 IDE
  • 只需按照我上面的描述更改设置,然后使用 UTF-8 编码再次另存为您的文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多