• ip address according country code in php?

    ip address according country code in php?

    Bronze Member Asked on January 16, 2017 in Php.
    Add Comment
  • 3 Answer(s)
    Best answer
    
    <?php
    
    function ip_visitor_country()
    
    {
    
    $client = @$_SERVER['HTTP_CLIENT_IP'];
    
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    
    $remote = $_SERVER['REMOTE_ADDR'];
    
    $country = "Unknown";
    
    if(filter_var($client, FILTER_VALIDATE_IP))
    
    {
    
    $ip = $client;
    
    }
    
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    
    {
    
    $ip = $forward;
    
    }
    
    else
    
    {
    
    $ip = $remote;
    
    }
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
    
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    $ip_data_in = curl_exec($ch); // string
    
    curl_close($ch);
    
    $ip_data = json_decode($ip_data_in,true);
    
    $ip_data = str_replace('&quot;', '"', $ip_data); // for PHP 5.2 see stackoverflow.com/questions/3110487/
    
    if($ip_data && $ip_data['geoplugin_countryCode'] != null) {
    
    $country = $ip_data['geoplugin_countryCode'];
    
    }
    
    return $country;
    
    }
    
    echo ip_visitor_country(); // output Coutry name
    
    ?>
    
    
    Bronze Member Answered on January 16, 2017.
    Add Comment

     

    <?php
    if (isset($_SERVER['HTTP_CLIENT_IP']))
    
    {
    
    $real_ip_adress = $_SERVER['HTTP_CLIENT_IP'];
    
    }
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    
    {
    
    $real_ip_adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    
    }
    
    else
    
    {
    
    $real_ip_adress = $_SERVER['REMOTE_ADDR'];
    
    }
    $cip = $real_ip_adress;
    
    $iptolocation = 'http://api.hostip.info/country.php?ip=' . $cip;
    
    $creatorlocation = file_get_contents($iptolocation);
    ?>
    
    

     

    Bronze Member Answered on January 16, 2017.
    Add Comment

     

    function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    
    $output = NULL;
    
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
    
    $ip = $_SERVER["REMOTE_ADDR"];
    
    if ($deep_detect) {
    
    if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
    
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    
    if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
    
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    
    }
    
    }
    
    $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    
    $support = array("country", "countrycode", "state", "region", "city", "location", "address");
    
    $continents = array(
    
    "AF" => "Africa",
    
    "AN" => "Antarctica",
    
    "AS" => "Asia",
    
    "EU" => "Europe",
    
    "OC" => "Australia (Oceania)",
    
    "NA" => "North America",
    
    "SA" => "South America"
    
    );
    
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
    
    $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
    
    if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
    
    switch ($purpose) {
    
    case "location":
    
    $output = array(
    
    "city" => @$ipdat->geoplugin_city,
    
    "state" => @$ipdat->geoplugin_regionName,
    
    "country" => @$ipdat->geoplugin_countryName,
    
    "country_code" => @$ipdat->geoplugin_countryCode,
    
    "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
    
    "continent_code" => @$ipdat->geoplugin_continentCode
    
    );
    
    break;
    
    case "address":
    
    $address = array($ipdat->geoplugin_countryName);
    
    if (@strlen($ipdat->geoplugin_regionName) >= 1)
    
    $address[] = $ipdat->geoplugin_regionName;
    
    if (@strlen($ipdat->geoplugin_city) >= 1)
    
    $address[] = $ipdat->geoplugin_city;
    
    $output = implode(", ", array_reverse($address));
    
    break;
    
    case "city":
    
    $output = @$ipdat->geoplugin_city;
    
    break;
    
    case "state":
    
    $output = @$ipdat->geoplugin_regionName;
    
    break;
    
    case "region":
    
    $output = @$ipdat->geoplugin_regionName;
    
    break;
    
    case "country":
    
    $output = @$ipdat->geoplugin_countryName;
    
    break;
    
    case "countrycode":
    
    $output = @$ipdat->geoplugin_countryCode;
    
    break;
    
    }
    
    }
    
    }
    
    return $output;
    
    }
     
    <?php
    echo ip_info("Visitor", "Country"); // India
    
    echo ip_info("Visitor", "Country Code"); // IN
    
    echo ip_info("Visitor", "State"); // Andhra Pradesh
    
    echo ip_info("Visitor", "City"); // Proddatur
    
    echo ip_info("Visitor", "Address"); // Proddatur, Andhra Pradesh, India
    print_r(ip_info("Visitor", "Location")); // Array ( [city] => Proddatur [state] => Andhra Pradesh [country] => India [country_code] => IN [continent] => Asia [continent_code] => AS )
    ?>
     
     
    
    

     

    Bronze Member Answered on January 16, 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.