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.
Result of php executing
Full code of Location Test.php
- <?php
- function get_client_ip() {
- $ipaddress = '';
- if (isset($_SERVER['HTTP_CLIENT_IP'])) {
- $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
- } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
- $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
- } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
- $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
- } else if (isset($_SERVER['HTTP_FORWARDED'])) {
- $ipaddress = $_SERVER['HTTP_FORWARDED'];
- } else if (isset($_SERVER['REMOTE_ADDR'])) {
- $ipaddress = $_SERVER['REMOTE_ADDR'];
- } else {
- $ipaddress = 'UNKNOWN';
- }
- return $ipaddress; }
- $UserPublicIP = get_client_ip();
- $json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
- $json = json_decode($json, true);
- $UserCity = $json['city'];
- $UserRegion = $json['region'];
- $UserCountry = $json['country'];
- $UserLoc = $json['loc'];
- $UserTimezone = $json['timezone'];
- //HTML Start:
- echo "<html>";
- echo "<head>";
- echo "</head>";
- echo "<title>Location Test</title>";
- echo "<body>";
- echo "Your IP Address is " . $UserPublicIP . ".<br>";
- echo "You are in " . $UserCity . ", " . $UserCountry . ".<br>";
- echo "Your timezone is " . $UserTimezone . ".<br>";
- echo "Your location is roughly " . $UserLoc . ".";
- ?>