[英]CakePHP Get IP Address
How can I get the client's IP address in CakePHP?我如何在 CakePHP 中获取客户端的 IP 地址? It'd be $_SERVER['REMOTE_ADDR']
in plain PHP.在普通的 PHP 中它是$_SERVER['REMOTE_ADDR']
。
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.我认为它就像所有$_SERVER
变量一样,可以使用env('VAR_NAME')
或 CakePHP 中的getClientIP()
,但它不会返回相同的结果。
Any ideas?有任何想法吗?
CakePHP 1.x :蛋糕PHP 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 : CakePHP 2.x 和 CakepPHP 3.x :
RequestHandler::getClientIp()
is deprecated; RequestHandler::getClientIp()
已弃用; you can get the client IP from the CakeRequest
object :您可以从CakeRequest
对象获取客户端 IP:
$this->request->clientIp();
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();
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()
可以作为Model替代Controller的$this->request
In cakephp 3.x在 cakephp 3.x 中
In your controller to get the client ip - $this->request->clientIp();在您的控制器中获取客户端 ip - $this->request->clientIp();
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
$this->request->clientIp();
// 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
You can use $this->request->clientIp();
您可以使用$this->request->clientIp();
to get the current visitor's IP address.获取当前访问者的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.