Get the country code and country name from where user access my site. Follow this example code:
$client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = @$_SERVER['REMOTE_ADDR']; $result = array('country'=>'', 'city'=>''); if(filter_var($client, FILTER_VALIDATE_IP)){ $ip = $client; }elseif(filter_var($forward, FILTER_VALIDATE_IP)){ $ip = $forward; }else{ $ip = $remote; } $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip)); if($query && $query['status'] == 'success') { echo 'from '.$query['country'].', '.$query['countryCode'].'!'; } else { echo 'Unable to get country'; }
Question and answer is powered by AnsPress.io