geoip targeting


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?
  2.  
  3. function geoip($ip = false, $api_key = 'cfea6a359ca8f852da3914055958db09bbcc63d4443b9ad21724dc3a9e8b4295'){
  4. if(!$ip) $ip = $_SERVER['REMOTE_ADDR'];
  5.  
  6. //you can get your own api key here: http://ipinfodb.com/register.php
  7. //this function attempts to call via curl, but will fall back to file_get_contents
  8. //if curl is not available.
  9. $url = "http://api.ipinfodb.com/v3/ip-city/?key={$api_key}&ip={$_SERVER['REMOTE_ADDR']}&format=json&callback=";
  10. if(function_exists('curl_init') && 1==0){
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  13. curl_setopt($ch, CURLOPT_URL, $url);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  16. $r = json_decode(curl_exec($ch), 1);
  17. curl_close($ch);
  18. }else{
  19. }
  20.  
  21. if(in_array(strtolower($r['regionName']), array('idaho', 'colorado'))){
  22. return true;
  23. }
  24. return false;
  25. }
  26.  
  27.  
  28. // Example usage of the function below:
  29. if(geoip()):
  30. ?>
  31.  
  32. is from idaho, or colorado.
  33.  
  34. <? else: ?>
  35.  
  36. nope not form idaho, or colorado.
  37.  
  38. <? endif; ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.