Last active
November 2, 2018 12:35
-
-
Save aliboy08/dbf25c93ae2a2f4ad0a2b2864da427f7 to your computer and use it in GitHub Desktop.
Get user location based on ip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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