Getting City/region from an IP-adress. (green rep)

Discussion in 'PHP' started by Dakuipje, Jul 12, 2007.

  1. #1
    Ok so im busy with statistics, now I need to get detailed information from an IP-adress such as region and city.

    After some research I found out this technique relies on large databases with information on millions of IP-adresses.

    Can someone either recommend me one ? Preferrable cheap or free but I will listen to all recommendation. If I find your information usefull I wont hesitate to leave you with some green rep.

    Maybe an alternative ?
     
    Dakuipje, Jul 12, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi

    Have you tried http://www.maxmind.com/app/ip-location ? They offer free and paid solutions - I believe their paid solutions are more accurate than their free solutions, buts it might be what you are after

    Brew
     
    Brewster, Jul 12, 2007 IP
  3. Dakuipje

    Dakuipje Peon

    Messages:
    931
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks.
    More are appriciated.

    (will leave rep when im home, somehow rep button doesnt work on this pc)
     
    Dakuipje, Jul 12, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    here's what i use though you can change it to easily fetch more than the country but that's all i needed so that's all i picked up.

    
    function ip2c($lookup_ip) {
    	$ip = $lookup_ip; 
    	$context=array('http' => array ('header'=> 'Range: bytes=1024-', ),); 
    	$xcontext = stream_context_create($context); 
    	$country = file_get_contents("http://api.hostip.info/get_html.php?ip=".$ip."&position=true",false,$xcontext); 
    	preg_match("/Country: (.*[^\n]+)/i",$country,$result); 
    	return $result[0];
    }
    
    PHP:
     
    ansi, Jul 12, 2007 IP
  5. Dakuipje

    Dakuipje Peon

    Messages:
    931
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Already managed to get the countries.
    That not the problem really.

    What do I need to change on the code to get city and region ?




    Sorry already found it. Thank you will add to your reputation ;)
     
    Dakuipje, Jul 12, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    replace the regular expression with this and it will return country, city/state, lat, long in an array

    
    /country: (.*?)\ncity: (.*?)\nlatitude: (.*?)\nlongitude: (.*?)$/i
    
    Code (markup):
    also note that this will only work in php5 because in php file_get_contents expects only 2 arguments, not 3.

    so for php4 remove

    
     $context=array('http' => array ('header'=> 'Range: bytes=1024-', ),); 
     $xcontext = stream_context_create($context); 
    
    Code (markup):
    and then the last argument in the file_get_contents line. "$xcontext"


    working php4 example:

    
    <?php
    	function ip2c($lookup_ip) {
    		$ip = $lookup_ip; 
    		$country = file_get_contents("http://api.hostip.info/get_html.php?ip=".$ip."&position=true",false); 
    		preg_match("/country: (.*?)\ncity: (.*?)\nlatitude: (.*?)\nlongitude: (.*?)$/i",$country,$result); 
    		return $result;
    	}
    
    	$data =  ip2c("24.26.73.230");
    	echo "Country: ".$data[1]."<br />";
    	echo "City / State: ".$data[2]."<br />";
    	echo "Latitude: ".$data[3]."<br />";
    	echo "Longitude: ".$data[4]."<br />";
    ?>
    
    PHP:
     
    ansi, Jul 12, 2007 IP