CakePHP Get IP Address

CakePHP 3.x usage:

//in controller
$ip = $this->request->clientIp();

CakePHP 2.x usage

//in controller
$this->request->ClientIp();

CakePHP 1.x usage

//in controller
RequestHandlerComponent::getClientIP();

CakePHP 1.x:

RequestHandlerComponent::getClientIp();

So to clarify:

public $components = array(
    'RequestHandler'
);

Then in the controller method:

$this->RequestHandler->getClientIp();

CakePHP 2.x & CakepPHP 3.x:

RequestHandler::getClientIp() is deprecated; you can get the client IP from the CakeRequest object:

$this->request->clientIp();

If you need to get the IP address from within a model, $this->request->getClientIp() won't work, throwing:

Error: Call to a member function clientIp() on a non-object

Use Router::getRequest()->clientIp() instead.

So basically, Router::getRequest() can serve as a Model's replacement of the Controller's $this->request


In cakephp 3.x

In your controller to get the client ip - $this->request->clientIp();