Search code examples
cakephpcakephp-2.3clientip

cakephp 2.3 not returning the user's real ip


I am trying to use the function "clientIp", to get the real IP of the user, but this is not returning anything. I'm using this:

$ipAddr = $this->request->clientIp();
print_r($ipAddr);die();

And print it " ::1 "

instead of the actual ip print, print it ::1

anyone ever had this problem, how can I solve?


Solution

  • Try this in cakephp 2.xx

    $this->request->clientIp();
    

    You can use native PHP server variable

    $_SERVER['REMOTE_ADDR']
    

    This is working example of my application on CakePhP

    function get_realIp(){
      if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
            $_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }elseif(isset($_SERVER['HTTP_X_REAL_IP']){
            $_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_REAL_IP'];
        }
    return $_SERVER['REMOTE_ADDR'];
    }
    

    let me know if i can help you more.