如何在PHP中使用IP地址查找国家/地区? [英] How to find country using IP address in PHP?

查看:309
本文介绍了如何在PHP中使用IP地址查找国家/地区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有国家/地区的IP范围。我正在检查此范围内的用户IP地址,如果为真,那么他能够看到该网站,否则他不会。
我使用以下代码,

I have IP ranges of the country. And I am checking the the user IP address within this ranges if true then he is able to see the site otherwise he wont. I have use following code,

$ip = $_SERVER['REMOTE_ADDR'];
   if(ip2long($ip) >= $ipstart && ip2long($ip) <= $ipend){
        return true;
    } 

我的问题是,如果有人来自不同的国家,假设ABC与他的笔记本电脑/笔记本到另一个国家假设XYZ然后将在上面的代码工作?在同一台笔记本电脑上。

My question is that if somebody comes from different country suppose ABC with his laptop/notebook to another country suppose XYZ then will above code works? on the same laptop.

以上怎么可能? ipv4和ipv6的情况?

哪个IP地址 $ _ SERVER ['REMOTE_ADDR'] fetch,它是机器IP还是连接IP,这样我们才知道他现在在这个国家?

Which IP address $_SERVER['REMOTE_ADDR'] fetch, will it be the machine IP or the connection IP, so that we can know that he now in this country?

谢谢

推荐答案

使用此API http://ipinfo.io

use this API http://ipinfo.io

function ip_details($IPaddress) 
    {
        $json       = file_get_contents("http://ipinfo.io/{$IPaddress}");
        $details    = json_decode($json);
        return $details;
    }

    $IPaddress  =   $_SERVER['REMOTE_ADDR'];

    $details    =   ip_details("$IPaddress");

    //echo $details->city;   #Tamilnadu  
    //echo $details->country;  
    //echo $details->org;      
    //echo $details->hostname; 

这篇关于如何在PHP中使用IP地址查找国家/地区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆