Get country location of an IP with native PHP
Asked Answered
I

8

17

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?

Intercommunion answered 18/3, 2010 at 7:46 Comment(0)
P
27

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

Pt answered 23/3, 2010 at 2:15 Comment(9)
That returns "UK" for me, regardless of the IP address I put at the end. (I am in the UK)Alarise
... the url you supplied is slightly wrong, corrected it is: api.hostip.info/country.php?ip=4.2.2.2Alarise
@Mailslut Yes, you are correct, sorry about that, don't know how I missed it. I've corrected it in the post. Cheers, --jedPt
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 :) )Intercommunion
@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.2Alarise
@Intercommunion what good API web-site did you find? i was using api.hostip.info as well, but i get to many XX answer for to many users.Alton
I'm still with hostip, my use for this isn't very critical.Intercommunion
This website is returning wrong results. I have tried VN ip but it return TW, US ip return VNMicrowatt
@IvanKuckir The data on hostip.info isn't always guaranteed to be accurate. However, you can go to the site with a web browser and manually provide a correction.Pt
A
18

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.

Alarise answered 18/3, 2010 at 8:8 Comment(0)
T
2

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.

Thermostatics answered 18/3, 2010 at 8:1 Comment(1)
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?Intercommunion
S
2

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 :)

Spencerianism answered 25/3, 2010 at 23:26 Comment(0)
D
1

Consider ipcountryphp (my site, my code, my honour) as it provides a local internet-lifetime freely updated database. It's fast and fully self-contained, pluggable into anything PHP 5.3, SQLite3 and beyond. Very fast seeks and no performance penalties.

Enough with shameless self-promotion, let's get serious:

Relying on querying remote services in real-time to get visitor country can become a major bottleneck for your site's functionality depending on the response speed of the queried server. As a rule of thumb you should never query external services for real-time site functionality (like page loading). Using APIs in the background is great but when you need to query the country of each visitor before the page is rendered, you open yourself up to a world of pain. And do keep in mind you're not the only one abusing free services :)

So queries to 3rd-party services stay in the background while only local functionality that relies on no 3rd-party go into the layers there users interact with. Just my slightly performance paranoid take on this :)

PS: Above mentioned script I wrote has IPv6 support too.

Damp answered 9/4, 2013 at 5:26 Comment(1)
What has happened to the site?Bedrock
D
0

Here is a site with a script i just used. The only problem is that you would probably every now and then need to regenerate IPs by yourself... which might be pain and tahts why everyone is telling you to use external API. But for me that wasnt solution as i was pulling like 50 IPs at once, which means i would probably get banned. So solution was to use my own script or to do saves to DB, but i was again pulling images from external sites. Anyway here is the site i found script on:

http://coding-talk.com/f29/country-flag-script-8882/

Devilkin answered 28/8, 2012 at 12:20 Comment(0)
C
0

here is also one of them. just change the IP to the variable:
http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=143.3.87.193

Contraindicate answered 17/12, 2013 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.