find county using ip address
Please support our PHP advertiser: Free Online Load Test - How many visitors can your web site handle?
![]() |
$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
$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
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/
There are several freebie GeoIP databases available.
http://www.maxmind.com/app/geoip_country
http://ip-to-country.webhosting.info/
•
•
Posts: 5
Reputation:
Solved Threads: 0
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.
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?
U can use the following function to get the country name from IP address.
php Syntax (Toggle Plain Text)
function countryCityFromIP($ipAddr) { //function to find country and city from IP address //Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url] //verify the IP address for the ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : ""; $ipDetail=array(); //initialize a blank array //get the XML result from hostip.info $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr); //get the city name inside the node <gml:name> and </gml:name> preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match); //assing the city name to the array $ipDetail['city']=$match[2]; //get the country name inside the node <countryName> and </countryName> preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches); //assign the country name to the $ipDetail array $ipDetail['country']=$matches[1]; //get the country name inside the node <countryName> and </countryName> preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match); $ipDetail['country_code']=$cc_match[1]; //assing the country code to array //return the array containing city, country and country code return $ipDetail; }
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)
![]() |
Similar Threads
Other Threads in the PHP Forum
- Intermittent internet connection is driving me nuts (Networking Hardware Configuration)
- IP address lookup.... (Network Security)
- windows xp user needs to know how to find deleted history (Windows Software)
- 2 computers (win 98 & xp) connected with switch, no internet on win98 computer (Networking Hardware Configuration)
- Print Server / VPN (Networking Hardware Configuration)
- user input and search (Java)
- Microsoft IE Offline Pop-ups (Web Browsers)
Other Threads in the PHP Forum
- Previous Thread: Which content management system
- Next Thread: Export Mysql data to CSV (also from diferent tables)
•
•
•
•
Views: 11284 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode