【问题标题】:GZIPing HTML in PHP stops Google Adsense adverts showingPHP 中的 GZIPing HTML 会阻止 Google Adsense 广告的展示
【发布时间】:2013-03-16 19:34:54
【问题描述】:

尝试在 PHP 中使用 GZIP 将 ~60kb 的索引页面减少到 ~10kb。除了没有显示 Google Adsense 广告之外,我还取得了成功。

为了您的考虑,我提供了同一页面的 2 个实时示例:

我使用的代码如下(你可以从这篇文章中认出它:PHP Manual GZip Encoding):

function _compress($data) {
    $supportsGzip = strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;

    if ( $supportsGzip ) {
        $content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9);
    } else {
        $content = $data;
    }

    $offset = 60 * 60;
    $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";

    header('Content-Encoding: gzip');
    header("content-type: text/html; charset: UTF-8");
    header("cache-control: must-revalidate");
    header( $expire );
    header( 'Content-Length: ' . strlen( $content ) );
    header('Vary: Accept-Encoding');

    echo $content;
}

ob_start();
include("index_standard.php");
$contents = ob_get_clean(); 
ob_end_flush();
_compress($contents);

这让我很困惑,因为其他 Javascript 不受影响。

作为旁注。我很想了解(并修复)这个错误,但如果不可能,我会考虑将我的广告位更改为异步加载方法作为备用计划。

【问题讨论】:

    标签: php gzip adsense


    【解决方案1】:

    在你的sesion_start()之前

    if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
    include("index_standard.php");
    ob_flush();
    

    【讨论】:

    • 谢谢!这是一种更新的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 2014-10-14
    • 2016-07-01
    相关资源
    最近更新 更多