location icon indicating copy to clipboard operation
location copied to clipboard

location by ip returns false

Open DevgirlTaabi opened this issue 3 years ago • 4 comments

I am trying to get location by $request->ip() and it is returning false on server.

DevgirlTaabi avatar Jan 15 '21 09:01 DevgirlTaabi

Sometimes it returns false and sometimes it returns data array , why ?

DevgirlTaabi avatar Jan 15 '21 09:01 DevgirlTaabi

I'm running into the same issue. It seems to be caused by the IP address that is returned. I think it's returning the load balancer IP and not the client IP, so there's no info returned with the request.

For example: This works: https://ipapi.co/104.185.180.215/json (hardcoded IP address used on local machine) For example, this returns nothing: https://ipapi.co/10.63.145.147/json - IP address returned by request()->ip()

Any thoughts on how to fix this?

Edit: Added more detail above... Also I may be wrong on the load balancer piece. Based off of some googling and me being confused 🤷‍♂️

evanave avatar Feb 21 '21 05:02 evanave

Solution! Use this to get the client's IP address: $ip = request()->header('X-Forwarded-For'); Instead Of: $ip = request()->ip(); Does anybody more knowledgeable than myself see any flaws with this?

evanave avatar Feb 21 '21 05:02 evanave

//Get client IP
//Since we are using Load Balancer calling Location::Get()
//Will always bring LB IP thus we need a custom method

public function getIp(){
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // just to be safe
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
    //It will return server/LB ip when no client ip found
    return request()->ip(); 
}

mr-muhammad-rehan avatar Feb 23 '21 12:02 mr-muhammad-rehan

Closing due to inactivity.

stevebauman avatar Aug 15 '22 13:08 stevebauman

I'm running into the same issue. It seems to be caused by the IP address that is returned. I think it's returning the load balancer IP and not the client IP, so there's no info returned with the request.

For example: This works: https://ipapi.co/104.185.180.215/json (hardcoded IP address used on local machine) For example, this returns nothing: https://ipapi.co/10.63.145.147/json - IP address returned by request()->ip()

Any thoughts on how to fix this?

Edit: Added more detail above... Also I may be wrong on the load balancer piece. Based off of some googling and me being confused 🤷‍♂️

The IP 10.63.145.147 is a reserved IP. It's used for local communications within a private network, hence there's no location associated with it.

ipapi-co avatar Apr 06 '24 13:04 ipapi-co