PHP get visitor location(by state)

What I am trying to do is use sessions to get a location(state) from all my visitors.

Yes I did search the net and found 2 different full scripts but I would like to understand how this works so I am back on sitepoint for help.

I am trying to obtain all of my visitors locations by state.

There are many API’s and modules out there that provide geo-location information from an IP address.

For example: http://www.ip2location.com/php.aspx

Keep in mind that geolocation by IP is an educated guess.

Yep. The only way to improve on it is to provide the user with the ability to correct as and where need be.

Michael you say geolocation by IP. How else can I do this other then IP and if there is any other way what is the difference in the two?

Newer browsers have the ability to disclose location information the user has provided to javascript - and such scripts can call back through Ajax to the server. Or, as pmw pointed out, you can explicitly gather that information from the user. That’s the only two choices at the moment. If its important to the site I’d use them all three falling back to the IP method as a last resort (or for guests).

I’ve used infosniper to do this on a site where I only wanted people in my area code to be able to create forum accounts. Think it is about $6 U.S. for 10K requests, which is very simple to implement. It returns, country, city (of dsl trunk), state, all kinds of other information in an array.

Another use is to auto-redirect people from known spamming countries to other sites or generate a 404.

If you’re happy with the results, but personally I wouldn’t use such a limit. People move into and out of areas, travel, and so on. Info sniper isn’t 100% accurate either - 100% accuracy isn’t possible with the current internet.

Another use is to auto-redirect people from known spamming countries to other sites or generate a 404.

That’s of dubious use since most spam comes from domestic zombie computers.

The IP address identifies the location of the ISP or proxy your visitor is using. With ISPs there is a good chance that the ISP is likely to be at least in the same country and with proxies there is a reasonable chance they may be on the same planet. Any attempt to define your visitor’s location more accurately than that is going to be less and less accurate the more closely you try to narrow it down.