Location Test - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Location Test.php

  1. <?php
  2. function get_client_ip() {
  3.     $ipaddress = '';
  4.     if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  5.         $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  6.     } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  7.         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  8.     } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
  9.         $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  10.     } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  11.         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  12.     } else if (isset($_SERVER['HTTP_FORWARDED'])) {
  13.         $ipaddress = $_SERVER['HTTP_FORWARDED'];
  14.     } else if (isset($_SERVER['REMOTE_ADDR'])) {
  15.         $ipaddress = $_SERVER['REMOTE_ADDR'];
  16.     } else {
  17.         $ipaddress = 'UNKNOWN';
  18.     }
  19.  
  20.     return $ipaddress; }
  21. $UserPublicIP   = get_client_ip();
  22. $json           = file_get_contents("http://ipinfo.io/$PublicIP/geo");
  23. $json           = json_decode($json, true);
  24. $UserCity       = $json['city'];
  25. $UserRegion     = $json['region'];
  26. $UserCountry    = $json['country'];
  27. $UserLoc        = $json['loc'];
  28. $UserTimezone   = $json['timezone'];
  29.  
  30. //HTML Start:
  31. echo "<html>";
  32. echo "<head>";
  33.     echo "</head>";
  34. echo "<title>Location Test</title>";
  35. echo "<body>";
  36.  
  37. echo "Your IP Address is " . $UserPublicIP . ".<br>";
  38. echo "You are in " . $UserCity . ", " . $UserCountry . ".<br>";
  39. echo "Your timezone is " . $UserTimezone . ".<br>";
  40. echo "Your location is roughly " . $UserLoc . ".";
  41.  
  42. ?>
File Description
  • Location Test
  • PHP Code
  • 09 Dec-2019
  • 1.42 Kb
You can Share it: