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=1821 qP1HchGTSHj71l8NDblPo6wQ7SK5EyXlSzQbGIVafQx0u7pyvYSWGPfvQD-05hT8 ea415235d3364ad1d3a66cc8ea125365f9868864&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
  • 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 LBG0uHVeHu_ByM5-VdADPwnrv4fnaMCJcCeD5n6_KURuXKqaJrizzCEJLgFa49kt 98bddfad28c48e5d8517673d9e351d6e27a914bc&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=1823 jQNo7jjXsv-uwswCP7wy-4-LQeWvdx5_VlYxoM7JW25efLXiD6KwBmh48M1-WtBq 188c0cb35d03ac8e19f819a1953cf56d29cf2634&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply