如何限制哪些国家/地区可以查看我的网站 ( PHP )
在 PHP 中有一个简单的方法可以做到这一点。 我想确保只有来自某些国家/地区的网络请求才能访问我的网站。
有任何想法吗?
In there an easy way to do this in PHP. I want to make sure that only web requests from certain countries are able to access my website.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 IP 地理定位数据库(有些是免费的)和 $_SERVER["REMOTE_ADDR"] 来获取访问者的 IP 地址。
http://www.maxmind.com/app/geolitecity 是免费(不太准确)版本商业的。
Use an IP geolocation database (some are free) and $_SERVER["REMOTE_ADDR"] to get the visitor's IP address.
http://www.maxmind.com/app/geolitecity is a free (less accurate) version of a commercial one.
和 Gilles 一样,我过去也使用过 MaxMind 的 GeoIP 功能——配置了 PECL 扩展来提高速度。 它工作得相当好,但是......
对这种事情的要求往往来自于不明白以这种方式可靠地确定访问者位置的人。 鼓励请求者了解它几乎没有用,这一点非常重要。 地理位置发生的典型情况如下:
……六个月后……
距离只有一步之遥“我可以拥有它,这样当用户在浏览器中右键单击时,会弹出一个小标志,上面写着“这些图像的版权归 Idiot Inc. 所有。” ?”
抱歉,今天显然心情很愤世嫉俗!
Like Gilles, I've used MaxMind's GeoIP stuff for this in the past - configured with the PECL extension for speed. It works reasonably well, but...
The requirement for this kind of thing tends to come from somebody that doesn't understand that it is impossible to reliably determine a visitors location in this way. It's very important that the person asking for it be encouraged to understand that it is almost useless. Typical thing that happens with geo-location in this:
...Six months later...
It's only one step on from there to "can I have it so that when a user right clicks in their browser a little sign pops up saying 'these images are copyright Idiot Inc.'?"
Sorry, obviously in a cynical mood today!
这两个答案(地理位置、用户代理)都可以工作,但也可能会失败。 有人可以使用代理服务器,或更改他们的用户代理。 Firefox 甚至有一个用于此目的的插件。
Both of the answers (geolocation, user agent) will work but can be defeated. Someone can use a proxy server, or change their user agent. Firefox even has a plugin for that purpose.
如果您使用 Ben Dowling 的答案并且代码不起作用,请尝试以下操作:
而不是:
if you use the answer of Ben Dowling and the code doesn't work, try this:
instead of:
还有一个 pear 包
http://pear.php.net/package/Net_Geo
There's also a pear package
http://pear.php.net/package/Net_Geo
有某些类可以检测用户代理的区域设置。 尝试类似的事情。 Zend Framework 有一个很棒的类来处理这个 Zend_Locale
There are certain classes that can detect the locale of the user agent. Try something like that. Zend Framework has a great class for this Zend_Locale