CakePHP Get IP Address

Aditya P Bhatt picture Aditya P Bhatt · Apr 15, 2011 · Viewed 29.4k times · Source

How can I get the client's IP address in CakePHP? It'd be $_SERVER['REMOTE_ADDR'] in plain PHP.

I thought it's like all $_SERVER vars and can be accessed using env('VAR_NAME'), or getClientIP() in CakePHP, but it doesn't return the same results.

Any ideas?

Answer

rich97 picture rich97 · Apr 15, 2011

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();