如何限制哪些国家/地区可以查看我的网站 ( PHP )

发布于 2024-07-06 19:44:42 字数 80 浏览 6 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ㄟ。诗瑗 2024-07-13 19:44:43

使用 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.

浪荡不羁 2024-07-13 19:44:43

和 Gilles 一样,我过去也使用过 MaxMind 的 GeoIP 功能——配置了 PECL 扩展来提高速度。 它工作得相当好,但是......

对这种事情的要求往往来自于不明白以这种方式可靠地确定访问者位置的人。 鼓励请求者了解它几乎没有用,这一点非常重要。 地理位置发生的典型情况如下:

<块引用>
<块引用>

客户端:我希望能够通过 IP 限制内容
Dev:你知道这是不可能可靠地做到的吗?
客户:啊是的,但这家公司说他们会卖给我一些可以做到这一点的东西
Dev:是的,但它不准确,而且很容易被规避,通常表明基于互联网的内容的商业模式很差
客户:你能做到吗?
开发者:不管怎样...


……六个月后……

<块引用>
<块引用>

客户:我的一些访问者一直抱怨他们看不到我的内容,而一些不应该看到我的内容的坏人却能够看到我的内容!
开发者:/我拍拍头


距离只有一步之遥“我可以拥有它,这样当用户在浏览器中右键单击时,会弹出一个小标志,上面写着“这些图像的版权归 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:

Client: I want to be able to restrict content by IP
Dev: You do know that that is impossible to do reliably?
Client: Ah yes, but this company say they will sell me something that will do it
Dev: Yes but it isn't accurate and is easy to circumvent and usually indicates a poor business model for internet based content
Client: Can you do it?
Dev: Whatever...

...Six months later...

Client: Some of my visitors have been complaining they can't see my content and some bad people who shouldn't see it have been able to!
Dev: /me slaps head

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!

聆听风音 2024-07-13 19:44:43

这两个答案(地理位置、用户代理)都可以工作,但也可能会失败。 有人可以使用代理服务器,或更改他们的用户代理。 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.

相权↑美人 2024-07-13 19:44:43

如果您使用 Ben Dowling 的答案并且代码不起作用,请尝试以下操作:

if (!in_array(substr($country, 0, 2), $allowed_countries)) {

而不是:

if(!in_array($country, $allowed_countries)) {

if you use the answer of Ben Dowling and the code doesn't work, try this:

if (!in_array(substr($country, 0, 2), $allowed_countries)) {

instead of:

if(!in_array($country, $allowed_countries)) {
难以启齿的温柔 2024-07-13 19:44:43

有某些类可以检测用户代理的区域设置。 尝试类似的事情。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文