/ Published in: PHP
This simple function allows you to getect a country code and city of your site visitors, using the www.geoplugin.net service. You may use this data to add default values in registration form or use it for any other purposes, like blocking access to your site from special regions.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function getLocationInfoByIp(){ $client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = @$_SERVER['REMOTE_ADDR']; $ip = $client; $ip = $forward; }else{ $ip = $remote; } if($ip_data && $ip_data->geoplugin_countryName != null){ $result['country'] = $ip_data->geoplugin_countryCode; $result['city'] = $ip_data->geoplugin_city; } return $result; } ?>
URL: http://www.apphp.com/index.php?snippet=php-get-country-by-ip