3

All tutorials I have seen have pointed towards functions like geoip_record_by_name. I always get this error: Fatal error: Call to undefined function geoip_record_by_name() in /home/<acct>/public_html/geoip.php on line <line>

I'm on "shared" hosting, meaning I don't have access to install the PECL geoip extension.

What are other (free!) ways to perform geoip lookups? Preferably ones which don't depend on an external service?

Cyclone
  • 17,939
  • 45
  • 124
  • 193
  • There's a few services that let you download a coarse-granularity database in csv format that'll make IPS down to country level. But otherwise you're stuck with an external service. You could probably recreate that geoip_*() function with some CURL calls, if the provider's API isn't too horrible. – Marc B Jun 27 '11 at 21:30
  • How do those services determine where your IP address is? How do THEY get the locations to begin with? How could I instead generate my own database? – Cyclone Jun 27 '11 at 21:31
  • Why make your own database when there are free APIs that will give you theirs? It would take a single person years to create their own database from scratch. – Steve Robbins Jun 27 '11 at 21:36
  • @imoda How are they even generated? – Cyclone Jun 27 '11 at 21:37
  • @Cyclone I wouldn't know the specifics. I assume it's a pretty meticulous process. – Steve Robbins Jun 27 '11 at 21:39
  • @imoda: And that process would entail...? Ping speedtests? Manual user data entry? Random guessing? Querying a government database? Dartboard? Flipping the bits using butterflies? I suppose it'd be possible to triangulate a user's location if you have enough servers to ping speedtest. – Cyclone Jun 27 '11 at 21:41
  • @Cyclone "I wouldn't know the specifics." – Steve Robbins Jun 27 '11 at 21:45
  • @Cyclone: they do the legwork and mine the IANA databases for IP block assignments, and follow things down the assignment chain and contact each block holder to ask "hey, where's your IPs?" – Marc B Jun 27 '11 at 21:46
  • @imoda: So your assumption has no basis on evidence? Sorry, I'm just really confused about how you could think it would take forever and be a meticulous process when you have no idea about the inner workings. It sounds like you're as in the dark as I am, lol. – Cyclone Jun 27 '11 at 21:47

2 Answers2

5

If you have MySQL server, there are IP databases that you can use and install for free, then do a $_SERVER['REMOTE_ADDR'] and run it against the database data.

i.e. (based on some arbitrary db)

<?php

    $result = mysql_query("SELECT *
                          FROM ips
                          WHERE ip = {$_SERVER['REMOTE_ADDR']}
                          LIMIT 1") or die(mysql_error());

    $row = mysql_fetch_assoc($result);

    $city = $row['city'];
    $state = $row['state'];
    $country = $row['country'];

?>

Some databases(Or just google it): http://www.ipinfodb.com/ip_database.php

EDIT

You can also do JSON/XML requests from other APIs and parse the data:

i.e. (Using ipinfodb.com again)

$doc->loadXML(file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=your_key&ip=" . $_SERVER['REMOTE_ADDR'] . "&timezone=false")); 

$country = $doc->getElementsByTagName('CountryName')->item(0)->nodeValue;
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
0

You can try hostip.info, Maxmind or ip2location to name a few. These are easy to use with PHP and can be used on shared hosting. Some of them have offline options as well.

webbiedave
  • 48,414
  • 8
  • 88
  • 101