限制访问特定国家/地区的 PHP 网站

发布于 2024-08-15 07:37:56 字数 46 浏览 7 评论 0原文

既然IP可以被欺骗,那么如何建立一个能够正确识别访问者所在国家的PHP网站呢?

Since IP can be spoofed, how can one build a PHP website that correctly identifies the visitor's country?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风柔一江水 2024-08-22 07:37:56

鉴于互联网的匿名性,这本质上是一个问题,但无论如何,通过欺骗 IP 地址获取在您所在国家/地区不合法的内容在技术上在大多数地方都是犯罪行为。

您应尽一切合理努力确保您的网站遵守媒体和信息的分发限制,但有些事情是无法防范的。最接近的方法是进行实际的实际地址验证,例如信用卡上的帐单地址或向某人邮寄用于注册的 PIN 码,但这两种选择都会代表用户或您自己产生费用。

That's inherently a problem given the anonymity of the internet, but spoofing IP addresses to obtain content not legally available in your country is technically a crime in most places anyways.

It's up to you to make every reasonable effort to ensure your site follows distribution restrictions on media and information, but there are some things that are just impractical to guard against. The closest you could get is by doing an actual physical address verification such as a billing address on a credit card or physically mailing someone a pin number for registration, but both of those options incur expenses on behalf of either the user or yourself.

无言温柔 2024-08-22 07:37:56

Joomla一直使用下面的函数来获取IP地址,这是一个非常通用的好函数,可以避免可能的作弊,你可以使用它:

function get_ip()
{
    $ip = false;

    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
         $ips = explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);

         if ($ip != false)
         {
              array_unshift($ips,$ip);
              $ip = false;
         }

         $count = count($ips);

         # exclude IP addresses reserved for LANs
         for ($i = 0; $i < $count; $i++)
         {
              if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i]))
              {
                  $ip = $ips[$i];
                  break;
              }
         }
    }

    if (false == $ip AND isset($_SERVER['REMOTE_ADDR']))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    return $ip;
}

Joomla has been using below function to get IP addresses, it is very versatile good function, that can avoid possible cheats, you can use it:

function get_ip()
{
    $ip = false;

    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
         $ips = explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);

         if ($ip != false)
         {
              array_unshift($ips,$ip);
              $ip = false;
         }

         $count = count($ips);

         # exclude IP addresses reserved for LANs
         for ($i = 0; $i < $count; $i++)
         {
              if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i]))
              {
                  $ip = $ips[$i];
                  break;
              }
         }
    }

    if (false == $ip AND isset($_SERVER['REMOTE_ADDR']))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

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