943,168 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 37385
  • PHP RSS
Jun 17th, 2006
0

IP-to-Country converter (php script)

Expand Post »
Hi,

I am looking for a PHP script that can take an ip address and return the country where that ip address belongs to.
If you know or have been using such a script, please let me know.

Thanks
Reputation Points: 10
Solved Threads: 5
Junior Poster
alpha2006 is offline Offline
116 posts
since Apr 2006
Jun 17th, 2006
0

Re: IP-to-Country converter (php script)

Quote originally posted by alpha2006 ...
Hi,

I am looking for a PHP script that can take an ip address and return the country where that ip address belongs to.
If you know or have been using such a script, please let me know.

Thanks
HI alpha2006,

What you need first is to get a database that matches IPs to countries/cities/locations.

Since IP to country is used alot in log analysis, you probably already have one installed on your php server if you rent one. Cpanel (common web based linux server administration panel) for example usually comes together with Awstats (analyses raw linux log files) which has an IP-country database.

Theres a couple listed at the ODP under log analysis:
http://dmoz.org/Computers/Software/I...ted_Utilities/

You can also find some at sourceforge.net or hotscripts.com

The php script of course will be specific to the database structure of the IP-to-Country Database you are using. Some databases will already have premade scripts too.

An easy way to get IPs converted to countries withought setting up the IP-to-country database and writing the php script for the db, is to use a webservice that does this for you and provides an API interface to geocoding (converting data to a location) IP addresses.

I've used http://www.hostip.info/ and they are really easy to use. Theres many others too.

With http://www.hostip.info/ if you dont require much functionality, you can just use a simple html based API for converting an IP to the contry flag.

Exaple:

[HTML]<A HREF="http://www.hostip.info">
<IMG SRC="http://api.hostip.info/flag.php" BORDER="0" ALT="IP Address Lookup">
</A>[/HTML]

Will automatically show the visitors contry flag.

To get just the contry via http, you can send http://www.hostip.info a get request to the url http://api.hostip.info/country.php

Example:

http://api.hostip.info/country.php

You can test this by clicking the link above.

The request to http://api.hostip.info/country.php returns the Country Code/Abbrev. in the HTTP Content of their HTTP Response.
So retrieving it via php is quite easy.

example:

[PHP]
<?php

$country = file_get_contents('http://api.hostip.info/country.php');
echo $country;


?>
[/PHP]

This however will return your server location, since the IP would be set as your server IP.

So you'll have to get the visitors IP via the REMOTE_ADDR which is available in PHP as a "predefined variable". Then append their IP to the country endpoint of their API.

Example:

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

Example PHP:

[PHP]
<?php

$country = file_get_contents('http://api.hostip.info/country.php?ip='.$_SERVER['REMOTE_ADDR'];
echo $country;

?>
[/PHP]

Note: usually you wouldn't just use $_SERVER['REMOTE_ADDR'] as that is sent to your server as a HTTP Header, and can be tampered with (unlikely but still). So you should treat it as user submitted data and "clean" it. Also, theres a few different predefined variables that get the IP, for different setups, theres a few functions out there just for getting the right one.

Using a web services is easier I believe, and avoids having a huge database and load on your server, but it is a bit slower and dependant on the availablility of the remote server that you are connecting to.

Hope that starts you off...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Apr 23rd, 2010
0
Re: IP-to-Country converter (php script)
I've only just started looking for the code that would do this and your reply was a real find. It worked like a charm. Thanks a whole bunch!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sandimcp is offline Offline
2 posts
since Apr 2010
Jun 28th, 2011
0
Re: IP-to-Country converter (php script)
But it returns me XX? What could be the reason?
Last edited by arshadshaikh; Jun 28th, 2011 at 12:55 pm. Reason: I could not enter correct data.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arshadshaikh is offline Offline
18 posts
since Sep 2008
Jun 28th, 2011
0
Re: IP-to-Country converter (php script)
Read the API information on hostip.info as APIs change.
http://www.hostip.info/use.html

The API endpoint/URL for retrieving IP information is now:
http://api.hostip.info/?ip={IP_ADDRESS}

eg:

http://api.hostip.info/?ip=12.215.42.19
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: onmouseover on select options not working in IE/Chrome/safari
Next Thread in PHP Forum Timeline: Help needed 3 tier-architecture





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC