I'm unable to get the IP address of the client which I need to determine his current location.
I've used request->ip(), $_SERVER['REMOTE_ADDR'] and I always get a 127.0.0.1 result which is not what I want.
What am I doing wrong?
I'm unable to get the IP address of the client which I need to determine his current location.
I've used request->ip(), $_SERVER['REMOTE_ADDR'] and I always get a 127.0.0.1 result which is not what I want.
What am I doing wrong?
Sometimes your clients use your application through a proxy, so you should not depend on $_SERVER['REMOTE_ADDR'].
Check out this link (with a little concern on securities):
How to get the client IP address in PHP?
request->ip()
will give you client IP. You're getting 127.0.0.1
in because you're trying to access your local project from the same machine.
I found a way how to fix it. But beware that you have to change it before going production!!
Read this part: https://laravel.com/docs/5.7/requests#configuring-trusted-proxies
And now just add this:
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array
*/
protected $proxies = '*';
Now request()->ip() gives you the correct ip
You can try this:
function get_ip() {
$keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
return $ip;
}
}
}
}
}
There is client IP address this code($_SERVER['REMOTE_ADDR']
) applied online project then it will work successfully . it will try..