Home >Backend Development >PHP Tutorial >How to Obtain a Client\'s IP Address in Laravel 5 ?
Obtain the Client's IP Address in Laravel 5
In PHP, obtaining a client's IP address is straightforward using $_SERVER["REMOTE_ADDR"]. However, in Laravel, this technique returns the server IP rather than the visitor's IP.
Solution:
To accurately acquire the client IP address in Laravel, use:
<code class="php">Request::ip();</code>
This function relies on Symfony's getClientIps method, which determines the client IP based on the following:
Trusted Proxies:
Forwarded Headers:
Client IP Headers:
Fallback:
If none of the above headers are available or trusted, the IP obtained from $_SERVER["REMOTE_ADDR"] is used.
Example:
<code class="php">$clientIp = Request::ip();</code>
This will provide the visitor's IP address, allowing you to implement IP-based functionality within your Laravel application.
The above is the detailed content of How to Obtain a Client\'s IP Address in Laravel 5 ?. For more information, please follow other related articles on the PHP Chinese website!