【问题标题】:How to display Google AdSense ads to search engine traffic only?如何仅向搜索引擎流量展示 Google AdSense 广告?
【发布时间】:2012-09-23 12:13:46
【问题描述】:

我只想向搜索引擎流量展示 Google AdSense 广告,而不向直接或来自 Facebook、Twitter、电子邮件链接等的常客展示广告...

这是我目前使用的代码,它似乎可以正常工作,但我还想改进代码以包含除 Google 之外的许多其他搜索引擎,例如 Bing、Yahoo、Ask 等。有人介意看看吗下面的代码并对其进行改进?

<?php
$ref = $_SERVER['HTTP_REFERER'];
if (preg_match("(google|yahoo|bing)", $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 ""
;
}
?>

【问题讨论】:

    标签: php preg-match adsense http-referer


    【解决方案1】:

    代码看起来相当不错。我的建议是使用/ 而不是( 作为模式分隔符,因为parens 也可以用于匹配组。您还可以添加 i 标志以使您的匹配不区分大小写。不需要你的 else 语句,因为它只是输出和空字符串。在您的 END HEREDOC 中还有一个额外的 &lt;/div&gt; 标记 - 您要确保打开和关闭都在您的 if 语句内部或外部。

    <?php
    $referrer = $_SERVER['HTTP_REFERER'];
    $my_domain = "example.com";
    $search_engines = "google|yahoo|bing|altavista|digg";
    $pattern = "((http(s)?:\/\/)(\w+?\.)?(?!{$my_domain})({$search_engines}))";
    if (preg_match("/{$pattern}/i", $referrer) != 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>
    END;
    } else {
        // Show something to visitors not referred by a search engine
    }
    ?>
    

    正则表达式模式的结果如下,用*标记的匹配:

    FALSE - http://example.com/google
    FALSE - http://example.com/google.com
    FALSE - http://www.example.com/google.com
    TRUE  - *http://google*.com/example.com
    TRUE  - *http://www1.google*.com/example.com
    TRUE  - *http://www.google*.com/example.com
    TRUE  - *http://images.google*.com/page
    TRUE  - *https://google*.com/example.com
    TRUE  - *https://www.google*.com/example.com
    

    【讨论】:

    • 哇,谢谢!听到我编写了“相当不错”的代码真是太酷了,太棒了。非常感谢你修改了代码并给了我这么好的解释。我真的很感激。 if 语句是向普通观众展示其他内容。我想我可以设法将其重新添加。再次感谢 doublesharp。
    • 嘿@doublesharp 我收到此错误:我收到此错误消息:Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_END_HEREDOC or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN in /var/www/xxxxx.com/public_html/xxx-xxx-xx.php on line 18
    • 修复它...END 不能缩进。在这里找到解决方案:codingforums.com/…
    • 我刚刚发现的一件事是,如果访问者点击网站内的链接,该链接的 url 结构中包含 google 一词,就会显示广告。
    • 对不起...只是使用stackoverflow编辑器并错过了我将其缩进。如果您打算向非搜索引擎访问者展示一些不同的东西,那么一定要留下else,删除它的唯一原因是它是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    相关资源
    最近更新 更多