CakePHP Get IP Address

后端 未结 6 2023
小鲜肉
小鲜肉 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:39

    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

提交回复
热议问题