3v4l.org

run code in 300+ PHP versions simultaneously
<?php function clientIpAddress(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ $address = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $address = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $address = $_SERVER['REMOTE_ADDR']; } return $address; } $ip_address = clientIpAddress(); echo "Client IP address: $ip_address"; // To geolocate the IP address, you can use an API service // Here's an example using the AbstractAPI IP Geolocation API $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); echo "<br>Country: ". $data['country']; echo "<br>City: ". $data['city']; echo "<br>Latitude: ". $data['latitude']; echo "<br>Longitude: ". $data['longitude']; ?>
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/30708 on line 8 Client IP address: Fatal error: Uncaught Error: Call to undefined function curl_init() in /in/30708:23 Stack trace: #0 {main} thrown in /in/30708 on line 23
Process exited with code 255.

preferences:
78.38 ms | 402 KiB | 62 Q