How to get client IP address in Laravel 5+
In standard PHP scripts, retrieving a visitor's IP is straightforward: you access the $_SERVER['REMOTE_ADDR'] superglobal. This variable holds the address of the last host that made a connection to your server. However, switching to a framework like Laravel introduces a layer of abstraction that often confuses developers accustomed to raw PHP logic. If you attempt to read $_SERVER['REMOTE_ADDR'] inside a Laravel controller or middleware and it returns the IP of your application server rather than the user's device, you are likely dealing with a proxy setup common in production environments.
Laravel handles this discrepancy gracefully through its Request facade. The framework provides a dedicated method to resolve the trusted client IP address, effectively shielding developers from the complexity of upstream proxies. You should use Request::ip() instead of accessing the raw server variable directly.
Under the hood, Laravel checks for specific headers injected by load balancers or reverse proxies, such as X-Forwarded-For. When a request passes through a proxy like Nginx, Apache with mod_proxy, or CloudFlare, the original client IP is often moved to one of these headers, while REMOTE_ADDR reflects the proxy's own IP. Laravel's implementation attempts to parse these headers in the correct order and returns the first non-empty value found.
use Illuminate\Support\Facades\Request;
class UserController extends Controller
{
public function show()
{
$clientIp = Request::ip();
// The method automatically handles trusted proxies
return response()->json(['client_ip' => $clientIp]);
}
}
While this approach works out of the box in most scenarios, it is crucial to understand how Laravel determines which headers to trust. By default, Laravel looks for X-Forwarded-For, X-Real-IP, and Client-IP. If your application runs behind a proxy that does not inject these headers correctly, or if you are running locally without any reverse proxy, Request::ip() will fall back to $_SERVER['REMOTE_ADDR']. This fallback behavior is why the raw PHP method seemed to work initially but fails in production setups involving proxies.
There is a potential pitfall regarding security and header spoofing. Because X-Forwarded-For headers can be manipulated by clients, they should only be trusted if the proxy acting as an intermediary is itself trusted. Laravel provides configuration options to define which proxy IPs are considered trusted via the APP_TRUSTED_PROXIES environment variable or the config/trusted_proxies.php file (though in older versions, this was handled strictly via environment variables). If your application runs behind multiple layers of proxies, you must ensure that only the immediate upstream proxies are marked as trusted to prevent a malicious client from spoofing their own IP.
For applications deployed on shared hosting or platforms like Laravel Forge and Vapor, the default configuration usually suffices because these services manage the proxy layer for you. However, if you deploy your own infrastructure, verify that your Nginx or Apache configuration is forwarding the necessary headers before relying on Request::ip().
If you need to access the raw header values for logging or debugging purposes, you can still inspect the specific keys directly from the request object, but be mindful that these values may contain multiple IPs separated by commas in the X-Forwarded-For header. Laravel parses this string and returns only the leftmost non-trusted IP by default to ensure accuracy.
Understanding the distinction between REMOTE_ADDR and the resolved client IP is essential for building robust applications. By leveraging Request::ip(), you align your code with Laravel's expectations regarding proxy handling, ensuring that your logs and analytics reflect the true origin of the traffic regardless of the infrastructure stack sitting in front of your application.
