programming.name Posted May 14, 2010 Share Posted May 14, 2010 Hi Can anyone please give me some script to get a users's nationality by IP address? I just want to develop a site that will be redirected the page by the nationality when accessing the site. In other words, if someone from Spain should see Spanish and so on so. Is there any special rule for assigning IP addresses for each country? Thanks Link to comment https://forums.phpfreaks.com/topic/201739-how-to-find-country-by-ip-address/ Share on other sites More sharing options...
Deoctor Posted May 14, 2010 Share Posted May 14, 2010 use this function <?php function countryCityFromIP($ipAddr) { //function to <strong class="highlight">find</strong> <strong class="highlight">country</strong> and city from <strong class="highlight">IP</strong> address //verify the <strong class="highlight">IP</strong> 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("[url=http://api.hostip.info/?ip=]http://api.hostip.info/?ip=[/url]".$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 <strong class="highlight">country</strong> name inside the node <countryName> and </countryName> preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches); //assign the <strong class="highlight">country</strong> name to the $ipDetail array $ipDetail['<strong class="highlight">country</strong>']=$matches[1]; //get the <strong class="highlight">country</strong> name inside the node <countryName> and </countryName> preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match); $ipDetail['country_code']=$cc_match[1]; //assing the <strong class="highlight">country</strong> code to array //return the array containing city, <strong class="highlight">country</strong> and <strong class="highlight">country</strong> code return $ipDetail; } ?> i didnt tested it though try it nd let me know Link to comment https://forums.phpfreaks.com/topic/201739-how-to-find-country-by-ip-address/#findComment-1058242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.