views:

467

answers:

5

Read on before you say this is a duplicate, it's not. (as far as I could see)

I want to get the county code in php from the client.

Yes I know you can do this using external sites or with the likes of "geoip_record_by_name" but I don't want to be dependent on an external site, and I can't install "pear" for php as im using shard Dreamhost hosting.

I thought I could just do something like this:

$output = shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\'');
echo "<pre>$output</pre>";

But dreamhost seems to have an old version of whois (4.7.5), so I get this error on allot of IPs:

Unknown AS number or IP network. Please upgrade this program.

So unless someone knows how to get a binary of a newer version of whois onto dreamhost im stuck.

Or is there another way I could get the country code from the client who is loading the page?

+2  A: 

Can you just install a copy of whois into your home directory and pass the full path into shell_exec? That way you're not bound to their upgrade schedule.

McJeff
I googled for a bit looking for a way to compile whois but didn't find anything. Nor did I find a pre-compiled version though it's probably because I was googling with the wrong keywords, any suggestions to where I may find the source or binary?
Mint
+12  A: 

MaxMind provide a free PHP GeoIP country lookup class (there is also a free country+city lookup one).

The bit you want is what is mentioned under "Pure PHP module". This doesn't require you to install anything, or be dependent on them, nor does it need any special PHP modules installed. Just save the GeoIP data file somewhere, then use their provided class to interact with it.

Mailslut
+3  A: 

Whois is just a client for the whois service, so technically you are still relying on an outside site. For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:

http://api.hostip.info/country.php?ip=4.2.2.2

returns

US

Good luck,

--jed

EDIT: @Mint Here is the link to the API on hostip.info: http://www.hostip.info/use.html

Jed Daniels
That returns "UK" for me, regardless of the IP address I put at the end. (I am in the UK)
Mailslut
... the url you supplied is slightly wrong, corrected it is:http://api.hostip.info/country.php?ip=4.2.2.2
Mailslut
@Mailslut Yes, you are correct, sorry about that, don't know how I missed it. I've corrected it in the post. Cheers, --jed
Jed Daniels
I tried it with my IP and it returns "xx" for the country, and in light of your info that whois relies on other servers (which I did know, just didn't really realize it at the time). I guess ill go with one of the online site API's (I came across a good one the other day, will have to hunt it down and post it here if no one comes up with a better idea :) )
Mint
@Jed - the link is still a little wrong, you missed out 'ip' between the '?' and '='.ie, country.php?ip=4.2.2.2 as opposed to country.php?=4.2.2.2
Mailslut
@Mailslut Thanks. Fixed.
Jed Daniels
A: 

An alternative, somewhat extreme solution to your problem would be to:

  1. Download the CSV format version of MaxMind's country database
  2. Strip out the information you don't need from the CSV with a script and ...
  3. ... generate a standard PHP file which contains a data structure containing the IP address as the key and the country code as the value.
  4. Include the resulting file in your usual project files and you now have a completely internal IP => country code lookup table.

The disadvantage is that, regularly, you would need to regenerate the PHP file from the latest version of the database. Also, it's a pretty nasty way of doing it in general and performance might not be the best :)

Shane Breatnach
+1  A: 

I have found that IP2location.com is offering PHP Module in the Sample Codes and API section. You can get more geographical location information and accurate data from the website.

SuperRomia
Ah yes that seems to work well. Thanks
Mint