仅向Facebook,Twitter访问者显示Google AdSense广告


Displaying Google AdSense Advertisements only to Facebook, Twitter Visitors

我正在使用下面的代码。当我将此代码放在 single.php 中时,它会显示带有内容的广告。我希望,当Facebook访问者访问我的网址时,它显示广告而不显示内容。当其他访问者通常访问该URL时,它应该显示内容。

<?php
    $ref = $_SERVER['HTTP_REFERER'];
    if (preg_match("(facebook)", $ref) != false) {
        echo <<<END
<script type="text/javascript"><!--
    google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
    /* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
    google_ad_slot = "xxxxxxxxxxxxxx";
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
END;
    }
    else {
        echo "";
    }
?>

如果single.php是错误的地方,我应该把它放在哪里?

请尝试一下:

<?php
    $ref = $_SERVER['HTTP_REFERER'];
    if (preg_match('facebook'.com', $ref) != false) {
?>
<script type="text/javascript"><!--
    google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
    /* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
    google_ad_slot = "xxxxxxxxxxxxxx";
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
</script>
<script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<?php
    } else {
        echo "";
    }
?>

检查推荐的另一种方法是使用 strpos 而不是 preg_match .

<?php
    $ref = $_SERVER['HTTP_REFERER'];
    if (strpos($ref, 'facebook.com') != false) {
?>
<script type="text/javascript"><!--
    google_ad_client = "xx-xx-xxxxxxxxxxxxxxxxxx";
    /* xxxxxxxx xxxxxx xxx xxx xxx xx xxxxxx */
    google_ad_slot = "xxxxxxxxxxxxxx";
    google_ad_width = xxx;
    google_ad_height = xxx;
    //-->
</script>
<script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<?php
    } else {
        echo "";
    }
?>