PHP Accessing the user's country (locale)

拟墨画扇 提交于 2020-02-02 13:48:27

问题


Is there a superglobal reference to the user's country or is using the IP against a database of IP to Country lookups the best way to go?


回答1:


i wish there were such thing as $_SERVER['HTTP_COUNTRY']... maybe in php99 ? ;) for the time being, http://php.net/manual/en/book.geoip.php is your best bet

however, if you're only interested in user's language, there is $_SERVER["HTTP_ACCEPT_LANGUAGE"]




回答2:


Here are some more options for ip to country lookups

http://www.hostip.info/

http://pear.php.net/package/Net_Geo




回答3:


Here an example of how to get country code from the IP. It's not a PHP superglobal, but it's not too had to implement either.

$country = strtoupper(file_get_contents("http://geoip.wtanaka.com/cc/{$_SERVER['REMOTE_ADDR']}"));


来源:https://stackoverflow.com/questions/1736024/php-accessing-the-users-country-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!