php CakePHP 获取 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5675265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:10:04  来源:igfitidea点击:

CakePHP Get IP Address

phpcakephpip-address

提问by Aditya P Bhatt

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

我如何在 CakePHP 中获取客户端的 IP 地址?它将$_SERVER['REMOTE_ADDR']在普通的 PHP 中。

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

我认为它就像所有的$_SERVER变量一样,可以使用env('VAR_NAME'), 或getClientIP()在 CakePHP 中访问,但它不会返回相同的结果。

Any ideas?

有任何想法吗?

回答by rich97

CakePHP 1.x:

蛋糕PHP 1.x:

RequestHandlerComponent::getClientIp();

RequestHandlerComponent::getClientIp();

So to clarify:

所以要澄清:

public $components = array(
    'RequestHandler'
);

Then in the controller method:

然后在控制器方法中:

$this->RequestHandler->getClientIp();

CakePHP 2.x & CakepPHP 3.x:

CakePHP 2.x 和 CakepPHP 3.x

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

RequestHandler::getClientIp()已弃用;您可以从CakeRequest对象中获取客户端 IP :

$this->request->clientIp();

回答by Aditya P Bhatt

CakePHP 3.x usage:

CakePHP 3.x 用法:

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

CakePHP 2.x usage

CakePHP 2.x 使用

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

CakePHP 1.x usage

CakePHP 1.x 使用

//in controller
RequestHandlerComponent::getClientIP();

回答by aexl

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

如果您需要从模型中获取 IP 地址,$this->request->getClientIp()则行不通,抛出:

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

错误:在非对象上调用成员函数 clientIp()

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

使用Router::getRequest()->clientIp()来代替。

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

所以基本上,Router::getRequest()可以作为示范“替代S中的控制器$this->request

回答by Avani Manvar

In cakephp 3.x

在 cakephp 3.x 中

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

在您的控制器中获取客户端 ip - $this->request->clientIp();

回答by Sehdev

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

您可以使用 $this->request->clientIp();来获取当前访问者的 IP 地址。

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

如需进一步参考 https://book.cakephp.org/3.0/en/controllers/request-response.html#reading-http-headers

回答by fitorec

Cakephp 3 have clientIP function in the class ServerRequest:

Cakephp 3 在类 ServerRequest 中有 clientIP 函数:

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

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.

例如,当您在本地环境中工作时,此函数返回值“:: 1”。

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

将它添加到 bootstrap.php 引导文件中是个好主意,因为您可以从任何地方访问它:

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

祝你好运,快乐编码!=D