Record ip address of visitors to my site

On my infinityfree free website I want to record the ip address of my visitors. Using the function below, when I access my site at home using different devices it always returns the same ip address - not the ip address of the device. How can I get the visitors ip address? Thanks in advance.

// Function to get the user IP address
function get_user_ip() {
    $ipaddress = "";

    if (isset($_SERVER['HTTP_CLIENT_IP'])):
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])):
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    elseif(isset($_SERVER['HTTP_X_FORWARDED'])):
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    elseif(isset($_SERVER['HTTP_FORWARDED_FOR'])):
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    elseif(isset($_SERVER['HTTP_FORWARDED'])):
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    elseif(isset($_SERVER['REMOTE_ADDR'])):
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else:
        $ipaddress = 'UNKNOWN';
	endif;

	return $ipaddress;	
}

What’s your website url?

jagger.epizy.com

And what IP does it return?

Always 92.7.184.117

You’ll see it right at the bottom of home page.

I see my IP at bottom, and that IP you listed is your IP, so it actually works fine??

1 Like

I have 2 PCs and a tablet, each has its own different ip address. Why does it always return the same address - or am I missing something. Is it the ip_address of my router? Thanks for your help by the way.

These are IPs for home network (192.xxx.xxx.xxx)

No, it is the IP assigned by your ISP. 92.7.184.117 for this case

2 Likes

Thanks KangJL. I’ve got it now. Do me a favour and login to jagger.epizy.com. I’ve created a function to record all visitors. It’s working when I login but not known when somebody else logs in.

1 Like

Just recorded logins from Slavonski Brod in Croatia so it’s working. Thanks to everyone for your help.

1 Like

Ah, that’s because IP records your wifi.

Due to your (and everyone’s) security, Device IP cannot be collected, the IP you’re seeing is which your internet provider gives you.

In case you have a domain (once in the future) and use Cloudflare
you can run into a problem that I had

and that is that my PHP was recorded IP from CF instead of user IP
because CF talked to the origin

So to get a real IP I have to use this massive…

<?php

function ip_in_range($ip, $range) {
    if (strpos($range, '/') == false)
        $range .= '/32';

    // $range is in IP/CIDR format eg 127.0.0.1/24
    list($range, $netmask) = explode('/', $range, 2);
    $range_decimal = ip2long($range);
    $ip_decimal = ip2long($ip);
    $wildcard_decimal = pow(2, (32 - $netmask)) - 1;
    $netmask_decimal = ~ $wildcard_decimal;
    return (($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal));
}

// oxy update list https://www.cloudflare.com/ips/
function _cloudflare_CheckIP($ip) {
    $cf_ips = array(
'173.245.48.0/20',
'103.21.244.0/22',
'103.22.200.0/22',
'103.31.4.0/22',
'141.101.64.0/18',
'108.162.192.0/18',
'190.93.240.0/20',
'188.114.96.0/20',
'197.234.240.0/22',
'198.41.128.0/17',
'162.158.0.0/15',
'172.64.0.0/13',
'131.0.72.0/22',
'104.16.0.0/13',
'104.24.0.0/14'

    );
    $is_cf_ip = false;
    foreach ($cf_ips as $cf_ip) {
        if (ip_in_range($ip, $cf_ip)) {
            $is_cf_ip = true;
            break;
        }
    } return $is_cf_ip;
}

function _cloudflare_Requests_Check() {
    $flag = true;

    if(!isset($_SERVER['HTTP_CF_CONNECTING_IP']))   $flag = false;
    if(!isset($_SERVER['HTTP_CF_IPCOUNTRY']))       $flag = false;
    if(!isset($_SERVER['HTTP_CF_RAY']))             $flag = false;
    if(!isset($_SERVER['HTTP_CF_VISITOR']))         $flag = false;
    return $flag;
}

function isCloudflare() {
    $ipCheck        = _cloudflare_CheckIP($_SERVER['REMOTE_ADDR']);
    $requestCheck   = _cloudflare_Requests_Check();
    return ($ipCheck && $requestCheck);
}

// Use when handling ip's
function getRequestIP() {
    $check = isCloudflare();

    if($check) {
        return $_SERVER['HTTP_CF_CONNECTING_IP'];
    } else {
        return $_SERVER['REMOTE_ADDR'];
    }
}


$ip = getRequestIP();
$cf = isCloudflare();

echo "Actual ip address is: ". $ip;

the IP range list needs to be updated (through the link I put in the comment)
from time to time if CF adds something new

4 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.