3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Get the client's IP address * * @return string The client's IP address */ function get_client_ip() { $ip_address = ''; if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip_address = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip_address = $_SERVER['REMOTE_ADDR']; } return $ip_address; } /** * Get the client's geolocation information * * @param string $ip_address The client's IP address * @return array The client's geolocation information */ function get_geolocation($ip_address) { $api_key = 'YOUR_API_KEY'; $url = "https://ipgeolocation.abstractapi.com/v1/?api_key=$api_key&ip=$ip_address"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); return $data; } // Get the client's IP address $ip_address = get_client_ip(); // Get the client's geolocation information $geolocation = get_geolocation($ip_address); // Display the results echo "Client IP address: $ip_address<br>"; echo "Country: ". $geolocation['country']. "<br>"; echo "City: ". $geolocation['city']. "<br>"; echo "Latitude: ". $geolocation['latitude']. "<br>"; echo "Longitude: ". $geolocation['longitude']. "<br>";
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Warning: Undefined array key "REMOTE_ADDR" in /in/qi122 on line 14 Fatal error: Uncaught Error: Call to undefined function curl_init() in /in/qi122:28 Stack trace: #0 /in/qi122(40): get_geolocation(NULL) #1 {main} thrown in /in/qi122 on line 28
Process exited with code 255.

preferences:
44.71 ms | 402 KiB | 62 Q