CakePHP Get IP Address

后端 未结 6 2026
小鲜肉
小鲜肉 2021-01-07 18:13

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

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 18:32

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

提交回复
热议问题