Display IP Address of users browser

< ?php
 // the IP address is unknown, so it equals to "%s" now.
 $urlTemplate = 'http://api.ip2location.com/?' . 'ip=%s&key=demo' .  '&package=WS10&format=json';
 // Current browser session
 $ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);

 $urlToCall = sprintf( $urlTemplate, $ip);

 $rawJson = file_get_contents( $urlToCall );

 $geoLocation = json_decode( $rawJson, true );

 echo 'Hello to ', $geoLocation['city_name'];

?>

Leave a Reply

Your email address will not be published. Required fields are marked *