PHP - IP'den Ülke Algılama

Vakana Çevrimdışı

Vakana

Administrator
Yönetici
KoCuce.Com
7 Mart 2012
12,797
297
83
Aşağıdaki kodları kilit ayar dosyanıza yada fonksiyon dosyasına ekleyin.

PHP:
[HIDE]

<?php 



/** 

* Country from IP Selector - Brought to you by Arda (SoulSmasher) 

* [url]http://www.soulsmasher.net[/url] 

* @author Roshan Bhattarai [url]http://roshanbh.com.np[/url] 

* @param str $ipAddr - Ip address to be checked 

* @return array ipDetail - an array about the ip 

*/ 

function countryCityFromIP($ipAddr) { 

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; 

} 





$IPDetail= countryCityFromIP($_SERVER['REMOTE_ADDR']); [/HIDE]
 
Üst