简体   繁体   中英

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.

您可以使用ip2location之类的服务,也可以只使用这种更简单的PHP 技术从访问者那里获取语言环境信息。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM