geo targeted script

ogah

New member
my simple script use freegeoip.net API, not need database.

PHP:
<?php
/*
script name: GeoTargeted ads
author: Ogah
site URL: http://indro-network.blogspot.com/2013/08/php-geo-targeted-content-atau-geo.html 
*/

$urlgeo = "http://www.freegeoip.net/json/".$_SERVER['REMOTE_ADDR'];
$geo = file_get_contents($urlgeo);
$jsgeo = json_decode($geo, true);
$negara = $jsgeo['country_code'];
if($negara == 'US') {
// put your ads or content that targeted to US visitor here
}
else {
// put content or ads for other country visitor here
}
?>
 

jaran

New member
Thanks. Its really usefull script after maxmind have disable their free service geolocation. The bad news if API usage is limited to 10,000 queries per hour. It will not suitable with your traffic have more than 10K UV from some IP country.
 

ogah

New member
not too bad because limitation is per hour not per days.
but very bad if 10,000 queries mean is total queries on freegeoip (not per clients) :cry:
 

jaran

New member
Hello guys, today I want to share free Geo IP script. Now, follow my instruction how to get it work,

- First, I want to use free service from freegeoip.net to get GEO IP. We can using JSON method to get the result.
Example,
Code:
http://freegeoip.net/json/
Result
Code:
{

    "ip": "YOUR IP",
    "country_code": "COUNTRY CODE",
    "country_name": "COUNTRY NAME",
    "region_code": "",
    "region_name": "",
    "city": "",
    "zipcode": "",
    "latitude": -5,
    "longitude": 120,
    "metro_code": "",
    "area_code": ""

}

Here we go the php script using CURL to parsing JSON result,
PHP:
//CURL FUNCTION
function get_curl($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}

//GET IP FUNCTION
function get_ip_address() 

                {
                    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // just to be safe

                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
  }

//JSON
$url = get_curl('http://freegeoip.net/json/'.get_ip_address());
$json = json_decode($url);

//PARSING JSON
$ip = $json->ip;
echo 'Your IP/Proxy : '.$ip;
?>
Now you can adding other info about Geo IP as what you need.
- To get country code
Code:
$json->country_code;
- To get Country Name
Code:
$json->country_name;
- Etc.