如何在某些国家/地区的页面中隐藏 google adsence 广告?

How to hide google adsence advert in a page for certain countries?

我是一名博主,在我的博客中投放了 google 广告。我不希望这些广告出现在某些国家/地区,是否可以在我的页面中使用任何其他代码来帮助我实现这一目标。

举个例子,我不想在德国展示广告。有什么建议吗?

您可以使用 http://www.geoplugin.net/

中的 API

例如。在 PHP

    $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".getRealIpAddr());
    $country = $xml->geoplugin_countryName ;
    if($country != "Germany")
    {?>
       <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <!-- Horizontal Big Banner -->
        <ins class="adsbygoogle"
             style="display:inline-block;width:970px;height:250px"
             data-ad-client="ca-pub-XXXXXXXXXXX"
             data-ad-slot="XXXXXXXX"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script>

    <?php
     }



//For getting IP Address

    function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }