Location detecting techniques for IP addresses

前端 未结 9 1204
甜味超标
甜味超标 2020-11-28 12:22

What are the location detecting techniques for IP addresses?
I know to look at the
$_SERVER[\'HTTP_ACCEPT_LANGUAGE\'] (not accurate but mostly useful to

9条回答
  •  天涯浪人
    2020-11-28 12:43

    If you looking for some copy - paste solution, i foudn this code :

        function countryCityFromIP($ipAddr)
    {
    //function to find country and city from IP address
    //Developed by Roshan Bhattarai http://roshanbh.com.np
    
    //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  and 
    preg_match("@(\s)*(.*?)@si",$xml,$match);
    
    //assing the city name to the array
    $ipDetail['city']=$match[2]; 
    
    //get the country name inside the node  and 
    preg_match("@(.*?)@si",$xml,$matches);
    
    //assign the country name to the $ipDetail array
    $ipDetail['country']=$matches[1];
    
    //get the country name inside the node  and 
    preg_match("@(.*?)@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;
    
    }
    

    Hope this helps somebody.

提交回复
热议问题