/ Published in: PHP
Expand |
Embed | Plain Text
<? 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="; $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{ } 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; ?>
You need to login to post a comment.
