插件91:根据IP确定用户所在的国家

<?php // Plug-in 91: Get Country From IP
/*
* 根据IP确定用户所在的国家
* 插件说明:
* 插件接受一个IP地址,然后返回该IP地址所在的国家。
* 如果操作失败,返回FALSE
* 它需要以下参数:
* $IP 一个IP地址
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$ip     = '127.0.0.1';
$result = PIPHP_GetCountryFromIP($ip);

if (!$result) echo "Could not identify location for '$ip'.";
else          echo "The location of '$ip' is: $result.";

function PIPHP_GetCountryFromIP($ip)
{
   // Plug-in 91: Get Country From IP
   //
   // This plug-in returns the country associated with a
   // supplied IP number. It requires this argument:
   //
   //    $ip: An IP address

   $iptemp = explode('.', $ip);
   $ipdec  = $iptemp[0] * 256 * 256 * 256 +
             $iptemp[1] * 256 * 256 +
             $iptemp[2] * 256 +
             $iptemp[3];
   $file  = file_get_contents('ips.txt');
   if (!strlen($file)) return FALSE;

   $lines = explode("\n", $file);

   foreach($lines as $line)
   {
      if (strlen($line))
      {
         $parts = explode(',', trim($line));

         if ($ipdec >= $parts[0] && $ipdec <= $parts[1])
            return $parts[2];
      }
   }

   return FALSE;
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值