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


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

get real IP address of user in PHP

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

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

Get visitors IP address with PHP

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

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

Read Also:  Stripe Payment Gateway Integration in Codeigniter Example

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


$allResultsInfo = @json_decode(file_get_contents("".$ip));

don’t Miss : PHP Get User IP Address

Complete Code: Get Visitor Information by IP Address in PHP

$allResultsInfo = @json_decode(file_get_contents("".$ip));  

if($allResultsInfo && $allResultsInfo->geoplugin_countryName != null){
	echo 'Visitor Country = '.$allResultsInfo->geoplugin_countryName.'<br/>';
	echo 'Visitor Country Code = '.$allResultsInfo->geoplugin_countryCode.'<br/>';
	echo 'Visitor City = '.$allResultsInfo->geoplugin_city.'<br/>';
	echo 'Visitor Region = '.$allResultsInfo->geoplugin_region.'<br/>';
	echo 'Visitor Latitude = '.$allResultsInfo->geoplugin_latitude.'<br/>';
	echo 'Visitor Longitude = '.$allResultsInfo->geoplugin_longitude.'<br/>';
	echo 'Visitor Timezone = '.$allResultsInfo->geoplugin_timezone.'<br/>';
	echo 'Visitor Continent Code = '.$allResultsInfo->geoplugin_continentCode.'<br/>';
	echo 'Visitor Continent Name = '.$allResultsInfo->geoplugin_continentName.'<br/>';
	echo 'Visitor Timezone = '.$allResultsInfo->geoplugin_timezone.'<br/>';
	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 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.