6

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;
    } 

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.

How will it be possible for the above scenario for ipv4 and ipv6?

[EDIT]

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?

Thanks

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Sachin
  • 1,273
  • 5
  • 16
  • 27
  • You will need a 3rd party API for IP country tables to look up from – Jake N Feb 19 '14 at 10:44
  • use this $countryName = geoip_country_name_by_name($_SERVER['REMOTE_ADDR']); PHP built method for finding country name – Haris Mar 15 '16 at 06:07
  • [IPLocate.io](https://www.iplocate.io) provides a free API: https://www.iplocate.io/api/lookup/8.8.8.8 - Example in PHP: `$result = json_decode(file_get_contents('https://www.iplocate.io/api/lookup/8.8.8.8')); echo $result->country;` - Disclaimer: I run this service. – ttarik Nov 16 '17 at 00:09

3 Answers3

6

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; 
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
1

Php doesn't have a function for this. You should either build a database or just use an external api to find that out.

http://ip-api.com Here is a good / free one. Just parse responses from that one.

cfv1000
  • 453
  • 3
  • 10
1

See this link

you can find country name from ip using this function,

function getLocationInfoByIp($ip_addr)
{
    $return_data  = array('country'=>'', 'city'=>'');
    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip_addr));
    if($ip_data && $ip_data->geoplugin_countryName != null)
    {
        $return_data['country'] = $ip_data->geoplugin_countryCode;
        $return_data['city'] = $ip_data->geoplugin_city;
    }
    return $return_data;
}

Pass IP address in this function and you will get country info...

For ip address, you can use one of following address as ip address,

$client_ip  = @$_SERVER['HTTP_CLIENT_IP'];
$forward_ip = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote_ip  = @$_SERVER['REMOTE_ADDR'];

Hope this will help you...

Akshay Paghdar
  • 3,599
  • 2
  • 22
  • 41
  • thanks for reply, I have a question that which IP address $_SERVER['REMOTE_ADDR'] fetches machine IP or the connection IP? – Sachin Feb 19 '14 at 10:59
  • you can see in link that i have attach in my answer... – Akshay Paghdar Feb 19 '14 at 11:27
  • I have used the above example, and I am shocked when seen the different results in different devices, Only `$_SERVER['REMOTE_ADDR'];` gives the result. For my desktop pc it gives the country **IN**, In my Mobile with Vodafone SIM it gives **GB** and Using Uninor SIM it gives me **US** – Sachin Feb 19 '14 at 12:47
  • That is because of different ip addresses... If you use SIM, your ip would be from any country...there is a big problem in using SIM services... So don't be shocked.. – Akshay Paghdar Feb 19 '14 at 13:31