Home >Backend Development >PHP Tutorial >How to Get the Client IP Address in Laravel without Falling Prey to Proxy Trickery?
Getting Client IP Address in Laravel 5
When attempting to acquire the client's IP address in Laravel, the $_SERVER["REMOTE_ADDR"] PHP function may inadvertently return the server's IP instead. To rectify this, Laravel provides a more robust method.
Laravel's IP Retrieval
Introducing Request::ip(), a Laravel function that effectively retrieves the client's IP address. Underneath the hood, it utilizes the getClientIps method from the Symfony Request Object.
public function getClientIps() { // ... Symfony method implementation }
This method considers trusted proxies, as evident in the headers and trusted proxy ranges configured in Laravel's request_config settings. Consequently, Request::ip() accurately obtains the visitor's IP address, regardless of intermediary proxies.
Therefore, to successfully retrieve the client's IP address in Laravel 5 , employ the following:
<code class="php">$clientIpAddress = request()->ip();</code>
The above is the detailed content of How to Get the Client IP Address in Laravel without Falling Prey to Proxy Trickery?. For more information, please follow other related articles on the PHP Chinese website!