CakePHP Get IP Address

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

    You can use $this->request->clientIp(); to get the current visitor's IP address.

    Cake\Http\ServerRequest::clientIp()
    Returns the current visitor’s IP address.
    

    For further reference https://book.cakephp.org/3.0/en/controllers/request-response.html#reading-http-headers

    0 讨论(0)
  • 2021-01-07 18:24

    Cakephp 3 have clientIP function in the class ServerRequest:

    https://github.com/cakephp/cakephp/blob/master/src/Http/ServerRequest.php#L578

    You can access:

    in a controller controller:

     $this->request->clientIp();
    

    in a controller controller:

     // firts add Router support
     use Cake\Routing\Router;
     // Use in a method
     Router::getRequest()->clientIp()
    

    I leave the function if you use a previous version of the framework or require some special behavior:

    public function clientIp()
    {
        if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
            $addresses = explode(',', $this->getEnv('HTTP_X_FORWARDED_FOR'));
            $ipaddr = end($addresses);
        } elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
            $ipaddr = $this->getEnv('HTTP_CLIENT_IP');
        } else {
            $ipaddr = $this->getEnv('REMOTE_ADDR');
        }
        return trim($ipaddr);
    }
    

    For example, this function returns the value ":: 1" when you work in a local environment.

    It is a good idea to add it in the bootstrap.php boot file, since you can access it from anywhere:

    function clientIp($defaultIP = '127.0.0.1') {
            $ipaddr = null;
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ipaddr = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ipaddr = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } else {
                $ipaddr = $_SERVER['REMOTE_ADDR'];
            }
            $ipaddr = trim($ipaddr);
            if ($ipaddr == '::1') {
                $ipaddr = $defaultIP;
            }
            return $ipaddr;
    }
    

    good luck and happy coding! =D

    0 讨论(0)
  • 2021-01-07 18:24

    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();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-07 18:45

    In cakephp 3.x

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

    0 讨论(0)
提交回复
热议问题