Geo Targeting and wordpress problem

Discussion in 'PHP' started by lumpy, Mar 11, 2011.

  1. #1
    Hey. I want to do something for one of my sites: not display AdSense results for users came from a certain country.

    To do that, I got 2 files from maxmind: GeoIP.dat and geoip.inc, which can be used to return the visitor's location based on ip. A friend gave me this code, which I doesn't work properly:

    In header.php:
    <?php
    include("geoip.inc");
    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
    $ip=$_SERVER['REMOTE_ADDR'];
    $cc=geoip_country_code_by_addr($gi, $ip);
    geoip_close($gi);
    ?>
    PHP:
    Wherever I want adsense (index.php):

    <?php if ($cc<>"US"): ?>
    The adsense code HERE
    <?php endif; ?>
    PHP:
    The problem is that if I place the code echo $cc in the header it will show my country, but it won't work in index.php

    Anyone knows why ?
     
    lumpy, Mar 11, 2011 IP