Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Last active November 2, 2018 12:35
Show Gist options
  • Save aliboy08/dbf25c93ae2a2f4ad0a2b2864da427f7 to your computer and use it in GitHub Desktop.
Save aliboy08/dbf25c93ae2a2f4ad0a2b2864da427f7 to your computer and use it in GitHub Desktop.
Get user location based on ip
<?php
function ff_get_user_location(){
// Get user ip
if( isset($_SERVER['HTTP_CF_CONNECTING_IP']) ) {
$user_ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} else {
if( isset($_SERVER['REMOTE_ADDR']) ) {
$user_ip = $_SERVER['REMOTE_ADDR'];
}
}
// Get user location based on ip
$ch = curl_init('http://ipinfo.io/'.$user_ip.'/json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return json_decode($result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment