Image for post
Image for post
Shah Goli — Tabriz

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

$_SERVER['REMOTE_ADDR']

Laravel provides a convenient way to retrieve client IP:

/* Illuminate/Http/Request.php */
request()->ip()
or/* symfony/http-foundation.php */
request()->getClientIp()

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…

About

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