location
location copied to clipboard
location by ip returns false
I am trying to get location by $request->ip() and it is returning false on server.
Sometimes it returns false and sometimes it returns data array , why ?
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 🤷♂️
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?
//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();
}
Closing due to inactivity.
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.