PHP: How do I find out if someone is from the US from his/her IP Address?
I don't want anything incredibly complicated. I am working on a client outside the US, and I just want a basic check to see if someone is accessing his page from the US or not. After that, I am going to default the language in either English or Spanish, after determining where the visitor is from.
开发者_运维问答Thanks.
Why not use internationalization with php? it works based on users browser settings. I wouldn't like to see a different language just because I am in a different country ;-) I had tough time when Google started showing search results and options in French when I was in Paris for a short time.
You can use a service like ip2location or you can just use this easier PHP technique to get locale information from the visitor.
MaxMind gives away a free copy of their IP address location database. It's accurate enough for country specific. I'd suggest using the binary version though as it will be much faster than loading it in something like MySQL. They provide a PHP API in addition to more sophisticated methods.
But as others have said, IP address is a poor thing to set language on. If I'm traveling in Mexico and load your page with my browser that requests English you shouldn't be sending me Spanish simply because I'm in Mexico.
精彩评论