Get Location By IP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Get Location By IP

Post by anjanesh »

If I goto http://whois.sc/xxx.xxx.xxx.xxxx I can get the location (approx) of that particular person. I can obviously parse this and store it.
But is there any way to this directly using PHP ?
I want to do it the way whois.sc has done it.

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I have a ip-to-country.csv list.

But it shows like this

Code: Select all

"50331648","69956103","US","USA","UNITED STATES"
"202031104","202033151","IN","IND","INDIA"
I thought of converting to hex and then converting all the 2 hex values back to dec to get the real IP but thats not it.

Any idea on how this format is stored ?

Thanks
Last edited by anjanesh on Tue Aug 02, 2005 10:36 am, edited 1 time in total.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i'm not mistaken there are some (native and extension) maxmind libraries to use with those files..
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

How complicated. Just convert from IP4 to long_ip before running the query. Here is the code to convert it.

Code: Select all

$ip4 = '12.34.56.78';
list($z, $y, $x, $w) = split('[[]]', $ip4);
$long_ip = ($w) + ($x * 256) + ($y * 256 * 256)+ ($z * 256 * 256 * 256);
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Thanks. ip2long returns -ve values too. Thanks bokehman.
But this csv file needs to be updated on a regular basis.

Is there anyway to hit some server's port directly just like the way Whois Servers are hit at port 43 for domain whois ?

Geobytes gives more accurate details like Region and City - Any API or db for that ?

Thanks
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

anjanesh wrote:Thanks. ip2long returns -ve values too. Thanks bokehman.
But this csv file needs to be updated on a regular basis.

Is there anyway to hit some server's port directly just like the way Whois Servers are hit at port 43 for domain whois ?

Geobytes gives more accurate details like Region and City - Any API or db for that ?

Thanks
If you want auto updating just set up a cron, but this seems a lot of trouble to me.

If you want greater accuracy than just the country you will have to pay and it's not cheap.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

bokehman wrote: If you want auto updating just set up a cron
Long term shot.
bokehman wrote: If you want greater accuracy than just the country you will have to pay and it's not cheap.
This is what im looking for. I just want to know how its done and I'll do the rest. Is there some place in IANA or ARIN to hit directly and get this result ?

How is geobytes doing it ?
Post Reply