In this Laravel 12 tutorial, you will learn how to get client IP address in Laravel 12 with practical examples. We will start by understanding what an IP address is and why it is important in web applications. Then, we’ll explore different ways to fetch the client IP using the Request object and the request() helper.
What is an IP Address?
An IP Address (Internet Protocol Address) is a unique identifier for each device on the internet or local network, enabling proper communication between systems. It works like a digital address that allows computers, servers, and mobile devices to communicate with each other.
There are two common versions of IP addresses:
- IPv4 : Example
192.168.1.1 - IPv6 : Example
2001:0db8:85a3:0000:0000:8a2e:0370:7334
In web applications, capturing the client’s IP address is useful for:
- User activity tracking
- Geo-location & analytics
- Security (blocking suspicious IPs, preventing abuse)
- Logging user actions
Laravel 12 How to Get Client IP Address Example
1. Using Request Object
Laravel provides the ip() method from the Illuminate\Http\Request class. You can inject Request into your controller method and call ip() like this:
Here, $request->ip() will return the client’s IP address.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function getIp(Request $request)
{
dd($request->ip());
return redirect()->back()->withSuccess("Data saved successfully");
}
}
Here, $request->ip() will return the client’s IP address.
2. Using request() Helper
Laravel also provides a global helper request(). You can directly use it without importing Request:
public function getIp(Request $request)
{
dd(request()->ip());
}
This is handy if you are fetching the IP inside routes, middleware, or other classes.
Read Also : Laravel 12 Socialite Login With Google Account Example
3. Using getClientIp() Method
Another option is to use the getClientIp() method, which returns the client’s IP in a similar way:
public function getIp(Request $request)
{
dd( $request->getClientIp());
//second way to get client ip via request()
dd(request()->ip());
}
4. Get Client IP In Blade
you can use above three methods to fetch client IP Address in blade file like this:
<p>{{request()->getClientIp()}}</p>
<p>{{request()->getClientIp()}}</p>
Handling Proxies and Load Balancers
The methods above work perfectly for direct connections, but when your Laravel 12 application runs behind proxies, load balancers, or CDNs (like CloudFlare, AWS ELB, or Azure), these methods may return the proxy’s IP address instead of the real client IP.
⚠️ The Problem
When your application is behind infrastructure like: CloudFlare CDN,AWS Elastic Load Balancer (ELB),Nginx reverse proxy,Docker containers with load balancers. The request()->ip() method will return the proxy’s IP (like 10.0.0.1 or 172.16.0.1) instead of the actual client IP.
Solution 1: Read X-Forwarded-For Header
This is a very common case when a Laravel application runs behind services like Cloudflare or Azure. In such setups, the client’s actual IP address is provided in the X-Forwarded-For header, which you can be parsed it directly like this:
$ip = $request->header('X-Forwarded-For');
To get requested and forwarded IP:
Route::get('/check-ip', function (Request $request) {
return response()->json([
'requested_ip' => $request->ip(), // IP Laravel sees directly
'forwarded_ip' => $request->header('X-Forwarded-For'), // Proxy header if available
]);
});
Solution 2: Use Laravel’s Trusted Proxies Middleware
A cleaner way is to configure Laravel’s built-in TrustedProxies middleware. Configure it in your bootstrap/app.php file:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Middleware\TrustProxies;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: [
'192.168.1.1',
'10.0.0.0/8',
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
import Illuminate\Http\Middleware\TrustProxies and specify proxies using trustProxies() method. If do not know IP Address you can use :
$middleware->trustProxies(at: '*');
You can also use proxy headers like this:
$middleware->trustProxies(headers: Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_AWS_ELB
);
Once configured, your original methods work correctly:
public function getIp(Request $request)
{
dd($request->ip()); // Now returns real client IP
dd($request->getClientIp());
}
Solution 3: Using Symfony’s setTrustedProxies
Since Laravel is built on Symfony components, you can also use the setTrustedProxies() method from symfony/http-foundation. You can also configure trusted proxies in your AppServiceProvider.
For example, in your App\Providers\AppServiceProvider : boot() method:
import this use Symfony\Component\HttpFoundation\Request; in your app\Providers\AppServiceProvider.php and add the following code in boot method:
use Symfony\Component\HttpFoundation\Request;
public function boot()
{
Request::setTrustedProxies(
['REMOTE_ADDR'],
Request::HEADER_X_FORWARDED_FOR
);
}
After adding this, the normal methods will return the real client IP:
request()->ip();
request()->getClientIp();
⚠️ Note: You can pass static proxy IPs in the first parameter.
But Cloudflare (and some cloud providers) frequently change their IP addresses. In that case, it’s safer to use Laravel’s TrustedProxies middleware with * instead.
Conclusion
In this tutorial, we explored how to get the client IP address in Laravel 12 with multiple approaches. We started by understanding what an IP address is, why it is important in web applications, and the difference between IPv4 and IPv6.
Then, we looked at different practical methods to retrieve the IP address:
- Using the $request->ip() method
- Using the request()->ip() helper
- Using the getClientIp() method
Finally, for applications running behind proxies, load balancers, or CDNs, we addressed how to retrieve the true client IP. This includes reading the X-Forwarded-For header and properly configuring Laravel’s TrustedProxies middleware—or using Symfony’s setTrustedProxies() in your service provider—for robust and accurate detection
By implementing these techniques, you can easily capture and use the client’s IP address for purposes such as logging, analytics, geo-location, or security checks.
Laravel makes it simple to work with client IPs, whether your app is running on a local server or behind proxies and load balancers.
Frequently Asked Questions (FAQs)
Q1: How do I get all forwarded IPs?
Use $request->ips() to see the chain. This is useful for debugging load balancers/CDNs and verifying that the expected original IP is present.
Q2: Can I use the IP address for rate limiting or authentication?
Yes, but be careful. IP addresses are not always unique—many users can share the same IP, and they can also change or be hidden behind proxies. It’s better to use IPs along with user IDs or session data. Also, make sure your proxy settings are correct so you always get the real IP.
Q3: How can I test whether proxy settings are correct?
- Add a temporary route that returns request()->ip() and request()->ips().
- Compare values against server access logs or an external “what is my IP” service.
- Check that the reported IP matches the real public client IP when going through the proxy.
Q4: Is it safe to use IP addresses for user identification?
Not entirely. IPs can change (dynamic IPs, VPNs, proxies), so they shouldn’t be your only method for authentication. They’re best used for logging, analytics, or adding an extra layer of security.
Q5: How can I log every request with client IP in Laravel?
You can create a middleware that logs request()->ip() for each incoming request, or use Laravel’s built-in logging system with custom log messages.
Q6: Is it safe to read X-Forwarded-For directly?
Avoid manual parsing in production. Prefer Laravel’s trusted proxy configuration and Request resolution. Directly trusting X-Forwarded-For without validating the proxy chain can allow spoofed values.
Q7: Will local testing always show 127.0.0.1?
Accessing from the same machine typically yields 127.0.0.1. Testing through a different device on the network or using a tunnel can show a different address, but production accuracy depends on trusted proxy configuration.
Q8: What’s the difference between ip(), ips(), and getClientIp()?
- ip(): returns the resolved client IP as a string.
- ips(): returns an array of IPs from forwarding headers (proxy chain), with the original client typically last.
- getClientIp(): Symfony HttpFoundation method that resolves similarly to ip() in most setups.