Saturday, December 15, 2018

detect ip and location





You can use a simple API from http://www.geoplugin.net/
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".getRealIpAddr());
echo $xml->geoplugin_countryName ;
 
 
echo "<pre>";
foreach ($xml as $key => $value)
{
    echo $key , "= " , $value ,  " \n" ;
}
echo "</pre>";
Function Used
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}




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




Geolocation is a great way to put relevant content to your web visitors and it is a technology that can be used in almost any application you can think of.
In this tutorial, we use the IP2Location™ Web Service to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our hosted web service.
Below are the sample codes written in PHP to query and display user’s geolocation information.
  1. <?php
  2. $urlTemplate = 'http://api.ip2location.com/?' . 'ip=%s&key=demo' . '&package=WS24&format=json';
  3. $host= gethostname();
  4. $ipAddress = gethostbyname($host);
  5. // replace the "%s" with real IP address
  6. $urlToCall = sprintf( $urlTemplate, $ipAddress);
  7. $rawJson = file_get_contents( $urlToCall );
  8. $geoLocation = json_decode( $rawJson, true );
  9. if(isset($geoLocation['city_name'])){
  10. if($geoLocation['city_name']!="-"){
  11. echo '<script language="javascript">';
  12. echo 'alert("Welcome Visitors from '.$geoLocation['city_name'].'")';
  13. echo '</script>';
  14. }else
  15. {
  16. echo '<center>You are in local server!</center><br>';
  17. echo '<script language="javascript">';
  18. echo 'alert("You are in local server!")';
  19. echo '</script>';
  20. }
  21. }else{
  22. echo 'IP Address parsing error!';
  23. }
  24. ?>
  25. <html>
  26. <head>
  27. <title>IP2Location Web Service</title>
  28. </head>
  29. <body>
  30. <div>
  31. <center>Hello World!</center><br>
  32. </div>
  33. <div>
  34. <center>Your IP address <?php echo $ipAddress; ?></center>
  35. <center>
  36. <?php
  37. if(isset($geoLocation['country_code'])&&isset($geoLocation['country_name'])&&isset($geoLocation['region_name'])&&isset($geoLocation['city_name'])&&isset($geoLocation['latitude'])&&isset($geoLocation['longitude'])&&isset($geoLocation['zip_code'])&&isset($geoLocation['time_zone'])){
  38. echo '<br>Country Code:'."\n". $geoLocation['country_code'] . "\n<br>";
  39. echo 'Country Name:'."\n". $geoLocation['country_name'] . "\n<br>";
  40. echo 'Region Name:'."\n". $geoLocation['region_name'] . "\n<br>";
  41. echo 'City Name:'."\n". $geoLocation['city_name'] . "\n<br>";
  42. echo 'Latitude:'."\n". $geoLocation['latitude'] . "\n<br>";
  43. echo 'Longitude:'."\n". $geoLocation['longitude'] . "\n<br>";
  44. echo 'Zip code:'."\n". $geoLocation['zip_code'] . "\n<br>";
  45. echo 'Time zone:'."\n". $geoLocation['time_zone'] . "\n<br>";
  46. }else{
  47. echo 'IP Address parsing error!';
  48. }
  49. ?>
  50. </center>
  51. </div>
  52. </body>
  53. </html>

No comments:

Post a Comment

form validation

function formsubmit ( ) { var empname = document .getElementById ( 'emp_name' ). value ; var email = document .getElem...