Advertisement
hexasoft

Display Visitor's GeoLocation Using Web Service for IPV4

May 16th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display Visitor's GeoLocation Using Web Service.*/
  3. /*              suggestions for different city locations across the country.                                    */
  4. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  5. /*https://www.ip2location.com/tutorials/display-visitors-geolocation-using-web-service-in-php                   */
  6. /****************************************************************************************************************/
  7.  
  8. <?php
  9.  $urlTemplate = 'http://api.ip2location.com/?' . 'ip=%s&key=demo' . '&package=WS24&format=json';
  10.  $host= gethostname();
  11.  $ipAddress = gethostbyname($host);
  12.  
  13.  // replace the "%s" with real IP address
  14.  $urlToCall = sprintf( $urlTemplate, $ipAddress);
  15.  
  16.  $rawJson = file_get_contents( $urlToCall );
  17.  
  18.  $geoLocation = json_decode( $rawJson, true );
  19.  
  20.  if(isset($geoLocation['city_name'])){
  21.  
  22.     if($geoLocation['city_name']!="-"){
  23.         echo '<script language="javascript">';
  24.         echo 'alert("Welcome Visitors from '.$geoLocation['city_name'].'")';
  25.         echo '</script>';
  26.     }else
  27.     {
  28.         echo '<center>You are in local server!</center><br>';
  29.         echo '<script language="javascript">';
  30.         echo 'alert("You are in local server!")';
  31.         echo '</script>';
  32.     }
  33.  }else{
  34.      echo 'IP Address parsing error!';
  35.  }
  36. ?>
  37. <html>
  38. <head>
  39. <title>IP2Location Web Service</title>
  40.     </head>
  41. <body>
  42. <div>
  43. <center>Hello World!</center><br>
  44. </div>
  45. <div>
  46. <center>Your IP address <?php echo $ipAddress; ?></center>
  47.       <center>
  48.       <?php
  49.       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'])){
  50.         echo '<br>Country Code:'."\n". $geoLocation['country_code'] . "\n<br>";
  51.         echo 'Country Name:'."\n". $geoLocation['country_name'] . "\n<br>";
  52.         echo 'Region Name:'."\n". $geoLocation['region_name'] . "\n<br>";
  53.         echo 'City Name:'."\n". $geoLocation['city_name'] . "\n<br>";
  54.         echo 'Latitude:'."\n". $geoLocation['latitude'] . "\n<br>";
  55.         echo 'Longitude:'."\n". $geoLocation['longitude'] . "\n<br>";
  56.         echo 'Zip code:'."\n". $geoLocation['zip_code'] . "\n<br>";
  57.         echo 'Time zone:'."\n". $geoLocation['time_zone'] . "\n<br>";
  58.       }else{
  59.           echo 'IP Address parsing error!';
  60.       }
  61.       ?>
  62.       </center>
  63. </div>
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement