RSS Forums RSS

find county using ip address

Reply
Posts: 139
Reputation: aarya is an unknown quantity at this point 
Solved Threads: 0
aarya's Avatar
aarya aarya is offline Offline
Junior Poster

find county using ip address

  #1  
May 10th, 2006
$url=$_POST['url'];
$ip = gethostbyname($url);
$long1 = ip2long($ip)

if ($long1 == -1 || $long1 == FALSE)
{ echo 'Invalid IP, please try again';
}
else {

print $url;
echo $ip ?>

bu this code i get ip address but i need the country name aslo with this ip address. is thers any function to get country?
thanks
AddThis Social Bookmark Button
Reply With Quote  
Posts: 86
Reputation: TopDogger is an unknown quantity at this point 
Solved Threads: 5
TopDogger's Avatar
TopDogger TopDogger is offline Offline
Junior Poster in Training

Re: find county using ip address

  #2  
May 16th, 2006
There is no function that identifies a country using the IP address. You will need to use a GeoIP (also called GeoLocation) database.

There are several freebie GeoIP databases available.
http://www.maxmind.com/app/geoip_country
http://ip-to-country.webhosting.info/
Reply With Quote  
Posts: 5
Reputation: attadaved is an unknown quantity at this point 
Solved Threads: 0
attadaved attadaved is offline Offline
Newbie Poster

Re: find county using ip address

  #3  
Apr 25th, 2008
Use <snipped>to get ip to country and other useful information.
Last edited by peter_budo : Apr 26th, 2008 at 7:18 am. Reason: Keep It Spam-Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reply With Quote  
Posts: 9
Reputation: kuldeep04 is an unknown quantity at this point 
Solved Threads: 0
kuldeep04's Avatar
kuldeep04 kuldeep04 is offline Offline
Newbie Poster

Re: find county using ip address

  #4  
Aug 8th, 2008
Hi Aatya,

U can use the following function to get the country name from IP address.

  1. function countryCityFromIP($ipAddr)
  2. {
  3. //function to find country and city from IP address
  4. //Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url]
  5.  
  6. //verify the IP address for the
  7. ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
  8. $ipDetail=array(); //initialize a blank array
  9.  
  10. //get the XML result from hostip.info
  11. $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
  12.  
  13. //get the city name inside the node <gml:name> and </gml:name>
  14. preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
  15.  
  16. //assing the city name to the array
  17. $ipDetail['city']=$match[2];
  18.  
  19. //get the country name inside the node <countryName> and </countryName>
  20. preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
  21.  
  22. //assign the country name to the $ipDetail array
  23. $ipDetail['country']=$matches[1];
  24.  
  25. //get the country name inside the node <countryName> and </countryName>
  26. preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
  27. $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
  28.  
  29. //return the array containing city, country and country code
  30. return $ipDetail;
  31.  
  32. }

plz try it let me know what's result?
Last edited by peter_budo : Aug 8th, 2008 at 5:43 am. Reason: Keep It Organized - please use [code] tags
Kuldeep (KD)
Reply With Quote  
Posts: 1
Reputation: Desolator is an unknown quantity at this point 
Solved Threads: 0
Desolator Desolator is offline Offline
Newbie Poster

Re: find county using ip address

  #5  
Mar 13th, 2009
Originally Posted by kuldeep04 View Post
Hi Aatya,

U can use the following function to get the country name from IP address.

~~~~~~

plz try it let me know what's result?


Hi kuldeep,

I used your script (with minor modifications - needed only to show the country) and it worked splendid.

thanks pal.
Last edited by Desolator : Mar 13th, 2009 at 10:45 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the PHP Forum
Views: 11284 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:20 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC