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

pickab00's avatar

$request->ip() returns 127.0.0.1

I am on laravel 5.6 and I am using Cloudways free 3 day trail to test my app. The thing is my code returns 127.0.0.1 but not the real client IP Address. Here is my controller:

$article->ip = $request->ip();

This returns 127.0.0.1 every time even though it is on a live server.

I also tried:

$article->ip = $request->getClientIp();

Which is basically the same as above. Can't think of anyway why this wouldn't work.

0 likes
11 replies
pickab00's avatar

@m7vm7v Unfortunately this does not return anything. It is returning nothing.

Cronix's avatar

Don't know anything about Cloudways, but see if this helps https://alan.fyi/real-ip-with-cloudways-and-cloudflare/

You'd probably have to check with cloudways. It could be possible they have you in front of cloudfare on the free tier, but don't allow you to change the settings. Im really not sure, it would be best to check with them.

pickab00's avatar

@m7vm7v Really sorry I was looking at the wrong thing. I had 2 Laravel setup. Your suggestion worked.

@Cronix Why won't fidelopers proxy work? I set the proxy to all and yet still it wouldn't work. Also about the above method, what are the drawbacks of this method? And is it safe? And will I have to change Illuminate\Http\Request.php to default if I migrate to some other server/hosting or such?

Cronix's avatar

Can't answer any of the above. It's best to ask Cloudways. I don't know anything about them or their setup. Free tiers are often very limited.

pickab00's avatar

That I get but. What about the stackoverflow answer linked above? What are the drawbacks of using a custom code inside of the Illuminate\Http\Request.php in laravel? Will it effect any other parts of laravel itself?

Cronix's avatar

Will it effect any other parts of laravel itself?

No, because it's a custom method that doesn't alter any existing methods and no existing code uses it.

But before you do that, why don't you just do something like this in a controller or something

dump($_SERVER);

and see if any of these keys have values other than 127.0.0.1?

'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'
pickab00's avatar

Yeah I asked this because the stack overflow answer said that the laravels throttle uses the Request->ip() as well. That is why I wanted to know if it would effect any other components.

martinbean's avatar

@pickab00 If you’re using something that proxies or load-balances your site, then the IP address will be of that proxy/load balancer, and not the end user.

I’ve not used Cloudways so don’t know what headers it sends, but you could try making your Laravel application trust proxies by updating your app/Http/Middleware/TrustProxies.php file. You can add:

protected $proxies = '*';

Please or to participate in this conversation.