Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Russelmhardy's avatar

Get client ip address in laravel

Hi i wanna ask about can i get Ip address of user by laravel i use $request->ip(); but it's gives me just ip of server !

0 likes
10 replies
Sinnbeck's avatar

What exactly do you get? And where is the server being run? Locally or is this production?

1 like
Sinnbeck's avatar

@Russelmhardy And $request->ip() gives you the IP of the server itself? Not the ip of a loadbalancer or simlar that is in front by any chance?

Sinnbeck's avatar

If it gives you the wrong one, you can use $request->ips() to get all ip's collected, and you can check if the correct one is further down the list. Perhaps you need $request->ips()[1]

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Russelmhardy .

yes , that what's happened

I asked if it was the server ip or a load balancer..

Anyways. Do you get the correct ip in the list?

1 like
martinbean's avatar

@russelmhardy If your production server is running behind a load balancer or proxy then yes, you’ll get the IP address of that load balancer/proxy because that is what’s connecting to your app.

You’ll need to update your TrustProxies middleware to read things like the scheme and end user’s IP address. The “nuclear” version is:

<?php

namespace App\Http\Middleware;

use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    protected $proxies = '*';
    protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB;
}

That will trust connections from all addresses. If the load balancer/proxy you’re using has a static IP address then it’d be better to specify that instead.

More information: https://laravel.com/docs/requests#configuring-trusted-proxies

2 likes

Please or to participate in this conversation.