Ayman_Alshiekh's avatar

get user ip insteade of application load balancer ip laravel and AWS

I run laravel application on AWS, I use Application Load Balancer.

Route::get('/what-is-my-ip', function(){ return request()->ip(); });

When I run this code, my ip doesn't show, it shows the load balancer's ip addresses.

My TrustProxies Middleware

protected $proxies = '*'; protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB;

0 likes
11 replies
rodrigo.pedra's avatar

Try running this code:

Route::get('/what-is-my-ip', function(){ 
    dd(request()->headers->all());
});

And check if the client's IP is in one of the headers.

Ayman_Alshiekh's avatar

@rodrigo.pedra everything works fine after some time without any change but until now user time zone not correct (i am using jamesmills/laravel-timezone package)

Ayman_Alshiekh's avatar

@rodrigo.pedra when my domain point to instance IP I can get the correct user timezone but when I change my DNS to make my domain point to Application Load Balancer i can't get correct user timezone

rodrigo.pedra's avatar
Level 56

@Ayman_Alshiekh well, you didn't answer if printing all headers has the user IP in some other proxied header.

It is also not clear if you provided any API keys for the geolocation services or the mastermind IP database to make the torann/geoip work properly.

So it is hard to help when you don't answer back any of the questions made.

If you want the user timezone, it is more reliable to send it from the browser and save it into the user's session. This is what I do on my projects.

A user can be traveling and have their preferred timezone set manually, or can be browsing through a proxy or VPN, so their IP location will be different than their real location.

You can get the client-side timezone with this JS code:

Intl.DateTimeFormat().resolvedOptions().timeZone

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions

Ayman_Alshiekh's avatar

@rodrigo.pedra thanks for your reply yes when I print request()->headers->all() it was contain my correct IP (the key is "x-forwarded-for")

I am not providing any API keys for the geolocation services or the mastermind IP database

rodrigo.pedra's avatar

@Ayman_Alshiekh well if you have the real IP and before you said the real IP was sufficient, than I think you are good, right?

As far as I know, geolocating requires either a external service or a database. Often these are paid services.

If you, or your client, are not willing on having these costs, and as it seems the timezone is what matters to your project, consider the JS solution, on first load if the session does not have a timezone key you output a JavaScript code that makes an AJAX call and writes the timezone to the user session using the snippet from my last response as the AJAX payload.

1 like
Sinnbeck's avatar

You can also get a list of ips found by using

$ips = request()->ips()
Ayman_Alshiekh's avatar

@Sinnbeck I can get the user IP correctly but I can't get the user timezone when my domain point to APB but when pointing to instance IP I get correct timezone

Please or to participate in this conversation.