Return to Snippet

Revision: 49515
at July 23, 2011 04:26 by jspicher


Initial Code
<?

function geoip($ip = false, $api_key = 'cfea6a359ca8f852da3914055958db09bbcc63d4443b9ad21724dc3a9e8b4295'){
    if(!$ip) $ip = $_SERVER['REMOTE_ADDR'];
    
    //you can get your own api key here: http://ipinfodb.com/register.php
    //this function attempts to call via curl, but will fall back to file_get_contents 
    //if curl is not available.
    $url = "http://api.ipinfodb.com/v3/ip-city/?key={$api_key}&ip={$_SERVER['REMOTE_ADDR']}&format=json&callback=";
    if(function_exists('curl_init') && 1==0){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $r = json_decode(curl_exec($ch), 1);
        curl_close($ch);
    }else{
        $r = json_decode(file_get_contents($url), 1);
    }
    
    if(in_array(strtolower($r['regionName']), array('idaho', 'colorado'))){
        return true;
    }
    return false;
}


// Example usage of the function below: 
if(geoip()):
?>

is from idaho, or colorado.

<? else: ?>

nope not form idaho, or colorado.

<? endif; ?>

Initial URL

                                

Initial Description

                                

Initial Title
geoip targeting

Initial Tags

                                

Initial Language
PHP