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 f开发者_StackOverflow社区its 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?
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: https://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service for details.
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/
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.
精彩评论