IP to Location?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

IP to Location?

Post by $var »

Assuming I have nabbed a users IP address with $_SERVER['SERVER_ADDR']; ...

what can i realistically do with this?

using code, not ARIN, how do i match it up with their geography to sync up ads, weather, and other custom options without asking for their ZIP/Postal codes?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

reliably, you can't.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

okay, ninja space goat i can understand that like with http_ref or any other global type server setting for that matter, there is a degree of unreliability, but this MaxMind was bang on for the IP I fed it.

Say, i was looking for an unreliable way to process these, what are some functions that might help me narrow geography for IP.

i have seen a few programs offering these services, displaying city, longitude, latitude... sometimes even postal codes
is this mindmax just querying the IANA? see the demo, it's pretty detailed.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

It was wrong for my IP address
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

How far off was it?
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

i think that mine is particularily easy to read, only because when you ping it it comes up as:
bas16-toronto63-1177909579

perhaps because the city name is in the response?
how far off was it for you JayBird?
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

i was given this link on another board to check out and it gives some more insight on the process.
http://www.ip2nation.com/

Code: Select all

<?php
	
	$server   = ''; // MySQL hostname
	$username = ''; // MySQL username
	$password = ''; // MySQL password
	$dbname   = ''; // MySQL db name
	
	
	$db = mysql_connect($server, $username, $password) or die(mysql_error());
	      mysql_select_db($dbname) or die(mysql_error());
			
	$sql = 'SELECT 
	            country
	        FROM 
	            ip2nation
	        WHERE 
	            ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
	        ORDER BY 
	            ip DESC 
	        LIMIT 0,1';
	
	list($country) = mysql_fetch_row(mysql_query($sql));
	
	switch ($country) {
		case 'se':
			// Get the swedish to a swedish newssite
			header('Location: http://www.thelocal.se/');
			exit;
		case 'us':
			// And let the folks from american go to CNN
			header('Location: http://www.cnn.com/');
			exit;
		default:
			// The rest can go to BBC
			header('Location: http://www.bbc.co.uk/');
			exit;
	}
	
?>
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

These scripts are just checking a list of IP ranges that are known to match up with certain locations. PEAR has a library that shows you how to use MaxMind's database to do this.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

MaxMind seemed to work relatively well.

It told me I'm in Manchester on my dial-up connection but when trying two static IPs against it it completely bailed.

My VDS is apparently in Norfolk but it's actually at Redbus Interhouse at London Docklands.
Our work servers are based in Manchester but it said they are in Newcastle (about 250 miles out).

I guess dynamic IPs are easier to trace since they'll all be dished out via a large ISP in a certain area.
Post Reply