Image for post
Image for post
Shah Goli — Tabriz

You can get the client real IP in PHP applications using following code:


Laravel provides a convenient way to retrieve client IP:

/* Illuminate/Http/Request.php */
or/* symfony/http-foundation.php */

But they are not working when your website is behind the Cloudflare or other proxies (e.g. a load balancer) and all return the Cloudflare Server IP.

However, there is a method in symfony/http-foundation/Request.php to bypass the proxy server IP. It is setTrustedProxies() method.

So, we can call this method in AppServiceProvider like this:

...use Symfony\Component\HttpFoundation\Request;...    public function boot()
/* This line set the Cloudflare's IP as a trusted proxy…


Khalil Laleh

Full-Stack Developer and Laravel Contributor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store