tags:

views:

59

answers:

3

I would like to show a sidebar advert to users only in a specific location (San Francisco). Would I sniff their IP, then if it fits in the San Fran IP area create an If/Else to show. If the IP != San Fran IP location then don't show?

Or do I not use IP to track their location these days?

+2  A: 

The only realistic option is to use the user's IP address. This will not always be correct (will be wrong e.g. for proxy or VPN users), but workable. For this you need a database that maps IPs to countries (geolocation database).

There are many geolocation services available, but mostly not for free. See this answer: http://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service for details.

sleske
A: 

Yes, you would use their IP address. You would need to download one of the following IP location databases and match the visitor's IP address to it:

http://ipinfodb.com/ip_database.php

That will give you their location.

tambler
A: 

The answer is the whois database. Use the whois command on Linux, for example:

$ host www.stackoverflow.com
www.stackoverflow.com is an alias for stackoverflow.com.
stackoverflow.com has address 69.59.196.211
stackoverflow.com mail is handled by 20 alt1.aspmx.l.google.com.
stackoverflow.com mail is handled by 30 alt2.aspmx.l.google.com.
stackoverflow.com mail is handled by 40 aspmx2.googlemail.com.
stackoverflow.com mail is handled by 50 aspmx3.googlemail.com.
stackoverflow.com mail is handled by 10 aspmx.l.google.com.
$ whois 69.59.196.211

OrgName:    Peak Inc.
OrgID:      PEAKIN-3
Address:    1600 SW Western Suite #180
City:       Corvallis
StateProv:  OR
PostalCode: 97333
Country:    US

...

Use the first "County" or "country" field. You will get the country of the network block which contains the IP. Try it at http://whois.domaintools.com/

Notinlist
Use exec() or popen() to run the command. http://hu.php.net/manual/en/function.exec.php http://hu.php.net/manual/en/function.popen.php
Notinlist
and i can port this into a php command to use on a website?
HollerTrain
Yes, but you should implement some caching of data. If you run the command with popen() then you can handle the output of the command as an input file.
Notinlist
What other options are there besides sniffing their IP?
HollerTrain
Not really. You can ask them to tell you :-)
Notinlist
Hmm. The reason I ask is I wrote the script that sniffs the IP, and if they are in SF, CA then it shows an image. However, the client I am writing this for is right in the middle of SF and their IP shows them as being in Texas. To make it worse, they show me six other sites that are able to directly pin point SF specific data to them. I'm lost here.
HollerTrain