php get ip address of visitor – How to Get IP Address of User in PHP?

php get ip address of visitor – The best methods to get the visitor IP address is using the REMOTE_ADDR in PHP. This post examples how to get visitors’ real IP address using PHP width source code examples and security remarks.

php get ip address of visitor

$_SERVER[‘REMOTE_ADDR’] –

echo 'Display User IP - '.$_SERVER['REMOTE_ADDR'];

Also Read : What Is My IP Address

get real IP address of user in PHP

function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}

echo 'User Real IP - '.getUserIpAddr();

Get visitors IP address with PHP

echo "visitors Remote addr: " . $_SERVER['REMOTE_ADDR']."
";
echo "visitors X Forward: " . $_SERVER['HTTP_X_FORWARDED_FOR']."
";
echo "visitors Clien IP: " . $_SERVER['HTTP_CLIENT_IP']."
";

simple PHP function that returns the probably real IP of the web site visitor:

function getUserIpAddr() {
$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}

return $ip;
}

Get Visitor Information by IP Address in PHP

$ip = $_SERVER['REMOTE_ADDR'];

$allResultsInfo = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

don’t Miss : PHP Get User IP Address

Complete Code: Get Visitor Information by IP Address in PHP

 echo 'Visitor Country = '.$allResultsInfo->geoplugin_countryName.'
';
echo 'Visitor Country Code = '.$allResultsInfo->geoplugin_countryCode.'
';
echo 'Visitor City = '.$allResultsInfo->geoplugin_city.'
';
echo 'Visitor Region = '.$allResultsInfo->geoplugin_region.'
';
echo 'Visitor Latitude = '.$allResultsInfo->geoplugin_latitude.'
';
echo 'Visitor Longitude = '.$allResultsInfo->geoplugin_longitude.'
';
echo 'Visitor Timezone = '.$allResultsInfo->geoplugin_timezone.'
';
echo 'Visitor Continent Code = '.$allResultsInfo->geoplugin_continentCode.'
';
echo 'Visitor Continent Name = '.$allResultsInfo->geoplugin_continentName.'
';
echo 'Visitor Timezone = '.$allResultsInfo->geoplugin_timezone.'
';
echo 'Visitor Currency Code = '.$allResultsInfo->geoplugin_currencyCode;
}
?>

Result of the above code –

Visitor Country = India
Visitor Country Code = IN
Visitor City = Rajkot
Visitor Region = Gujarat
Visitor Latitude = 22.2916
Visitor Longitude = 70.7932
Visitor Timezone = Asia/Kolkata
Visitor Continent Code = AS
Visitor Continent Name = Asia
Visitor Timezone = Asia/Kolkata
Visitor Currency Code = INR

I hope you get an idea about php get ip address of visitor.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment