Symfony2-如何获取外部(公共)和内部(本地)IP


Symfony2 - how to get external (public) and internal (local) IPs?

我知道

$this->container->get('request_stack')->getMasterRequest()->getClientIp();

返回用户的内部IP,如:192.168.1.100,但我想获得更像:84.56.69.5 的外部IP

像www.get-ip.me这样的网站如何知道我的ip,以及我如何在symfony3中获得它?

Request类的getClientIp应该已经检索到"外部IP"。

192.168.1.100更像是代理的IP。你可以用getClientIps:看到所有这些

var_dump($this->container->get('request_stack')->getMasterRequest()->getClientIps());

如果192.168.1.100是反向代理的IP,则编辑您的配置如下:

framework:
    trusted_proxies:  [192.168.1.100]

并使用getClientIp检索用户的IP。