How to Get Client IP Address in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to get client IP address in Laravel.

 

How to get client IP address in Laravel

In Laravel, you can retrieve the client IP address in the following way:

 

// In a controller or a middleware

public function myControllerAction(Request $request)
{
    $clientIP = $request->ip(); // Returns the client IP address
    // ...
}

 

Alternatively, you can also retrieve the client IP address using the server method of the Request object:

// In a controller or a middleware

public function myControllerAction(Request $request)
{
    $clientIP = $request->server('REMOTE_ADDR'); // Returns the client IP address
    // ...
}

 

Note: The ip() method provided by Laravel takes into account a possible reverse proxy or load balancer in front of your application, and returns the correct client IP address even in this scenario.

Comments 0

Leave a comment