How to get real client IP address when you use Cloudflare


In almost all case, we can get real client IP address with $_SERVER[“HTTP_CF_CONNECTING_IP”] and $_SERVER[“REMOTE_ADDR”]

function getIP(){
    return !empty($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
}

But in some special cases we will get proxy IP like: 66.249.82.158, check $_SERVER we will see:

$_SERVER['HTTP_FORWARDED'] = "for=xx.xx.xx.xx";
$_SERVER['HTTP_X_FORWARDED_FOR'] = "xx.xx.xx.xx,66.249.82.158";

So we have 2 methods:
Method 1:

function getRealIP(){
    if(!empty($_SERVER['HTTP_FORWARDED'])){
        return str_replace("for=","",$_SERVER['HTTP_FORWARDED']);
    }
    return !empty($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
}

Method 2:

function getRealIP(){
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $temp = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']);
        return $temp[0];
    }
    return !empty($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
}

Recent search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1841 eMzLvmAtwAde7weyjTvYOcCxd4MFDvSdpzVuHhasLGfJnkrv-AhhPAlUqXT3yLB8 16eb6d0029ec17e64a3711192378ab6cc0afd145&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1827 _fGAnyWI-0vPGeyqf_HZhoC7_PjUbyvAMwVPq_vvyfqmYXkIEhpnB7Nch3cvGvba 94d4cf059aafb1e417d1cb5b5624760f5a9cc5a5&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1827 mljbiKhQECOC9dvx8Zva-ITXJpIdzKfplP-YJDfzGWiBZhU4l7X1ux58fCeK8Opk d88c0a2b4603be3b48c0d9611c4c2390962cfd64&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1827 E5me5CQa6RsMtQHHzwhPAXpvFUgRFwHaut1JuoO8mDlv2f0SIftpDx1uNFLIQhcU 30e3a688c29bf9ae5773de68cf9d08437d63f6ee&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1824 RJCgUU5KSFPoEpBo96XzhWS4HF1a6ssMJLn72fO1u1Wv4u2LellVXfIdwA9GU8Uq a88e54dd9d05f947745d66b281ebf35bb1a79142&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1823 jQNo7jjXsv-uwswCP7wy-4-LQeWvdx5_VlYxoM7JW25efLXiD6KwBmh48M1-WtBq 188c0cb35d03ac8e19f819a1953cf56d29cf2634&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 W2EmCp6yaF0C3ROh7JRL0hmXXppvWcd4sHmWJ86gr_ohLbYVcklGTCA4dsfGRVXH ba670bf98e4f92d8265169f64f504fae410dff8f&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 LBG0uHVeHu_ByM5-VdADPwnrv4fnaMCJcCeD5n6_KURuXKqaJrizzCEJLgFa49kt 98bddfad28c48e5d8517673d9e351d6e27a914bc&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 5BnG5o_n1kkiYjT7LmnKfxiqQ_3elHiB3djcvDh7LsQtLdktfarQlF1jWC26T9RN 15aa840e94fc4126bba6ac7cd9c440c84fe1633d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 0JLJ4ht1ZqIXW-N_nPWvzjae8SkvE3PZWtTFY3X-Fj-kHUSx8-NBVEel7FaiRG3T 80b2a4d3013c7494581cfaefe609a9c1bef691af&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply