Created
April 4, 2021 14:23
-
-
Save adicahyaludin/cdcbb3fcc26b80308660955886010b89 to your computer and use it in GitHub Desktop.
get visitor ip public & geolocation by API PHP
This file contains 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 vcwpk_get_visitor_ip() { | |
$ipaddress = file_get_contents('https://api.ipify.org'); | |
if ( ! $ipaddress ) : | |
if (getenv('HTTP_CLIENT_IP')) | |
$ipaddress = getenv('HTTP_CLIENT_IP'); | |
else if(getenv('HTTP_X_FORWARDED_FOR')) | |
$ipaddress = getenv('HTTP_X_FORWARDED_FOR'); | |
else if(getenv('HTTP_X_FORWARDED')) | |
$ipaddress = getenv('HTTP_X_FORWARDED'); | |
else if(getenv('HTTP_FORWARDED_FOR')) | |
$ipaddress = getenv('HTTP_FORWARDED_FOR'); | |
else if(getenv('HTTP_FORWARDED')) | |
$ipaddress = getenv('HTTP_FORWARDED'); | |
else if(getenv('REMOTE_ADDR')) | |
$ipaddress = getenv('REMOTE_ADDR'); | |
else | |
$ipaddress = ''; | |
endif; | |
return $ipaddress; | |
} | |
function vcwpk_get_visitor_location() { | |
$location = []; | |
$ip = vcwpk_get_visitor_ip(); | |
if ( $ip ) : | |
// $json = file_get_contents('https://api.snoopi.io/$PublicIP?apikey=[YOUR API KEY]'); | |
// $json = json_decode($json ,true); | |
// $json = file_get_contents('http://api.ipstack.com/$PublicIP?access_key=[YOUR API KEY]'); | |
// $json = json_decode($json ,true); | |
$json = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$ip)); | |
if ( $json ) : | |
$location = $json; | |
endif; | |
endif; | |
return $location; | |
} | |
$location = vcwpk_get_visitor_location(); | |
echo '<pre>'.print_r($location,1).'</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment