How do I get the user IP address in Symfony2 controller?

SymfonyIp Address

Symfony Problem Overview


I need to store the IP address of the users who comment, in the database after form submission.

Is there any symfony2 function to get the IP? Or any other way to get the IP?

Symfony Solutions


Solution 1 - Symfony

You can get the client IP using Request service:

$container->get('request')->getClientIp();

Solution 2 - Symfony

In Symfony before 2.3 $this->container->get('request')->getClientIp() works only inside of master request controller. In sub-request controller this always returns 127.0.0.1. In case your project uses sub-requests with Symfony 2.2, the bullet-proof solution is to create kernel.request listener and save the IP from the master request in it.

In Symfony 2.3 this was fixed so for internal sub-requests the real IP is pushed to the list of proxies, see https://github.com/symfony/symfony/commit/2f3b33a630727cbc9cf21262817240a72a8dae0c So you need to add 127.0.0.1 to trusted_proxies configuration parameter to get client ip from the Request in sub-requests in Symfony 2.3+, but you shouldn't do this on shared hosting for security reasons.

Also, 127.0.0.1 had to be added to trusted_proxies explicitly if built-in HTTP cache (AppCache in web/app.php) was used before Symfony 2.3.20. This cache tries to look like a real reverse-proxy and modifies some headers of master request. Fixed in https://github.com/symfony/symfony/commit/902efb8a84e8f0acf6a63e09afa08e3dcdd80fb9

Since Symfony 2.4 and in 3.x the preferred way to access current request is either using request_stack service

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

or injecting Request into controller, see http://symfony.com/doc/current/book/controller.html#the-request-as-a-controller-argument

public function indexAction(Request $request)
{
    $ip = $request->getClientIp();
}

But the concern about excluding 127.0.0.1 when used in sub-requests still apply, but now you may try to explicitly refer to master request using

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

Solution 3 - Symfony

FYI, As of Symfony 2.0 Request::getClientIp the $proxy parameter is deprecated. It will be removed in Symfony 2.3.

You can either use

$container->get('request')->server->get("REMOTE_ADDR");

or as @meze answer

$container->get('request')->getClientIp();

Solution 4 - Symfony

For Symfony 2.6+ use the following code (within your controller:

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

Solution 5 - Symfony

there is also another way to inject the current client IP into any service or method call:

acme.currentIP:
    class: some\service\className
    arguments:
        - "@=service('request_stack').getCurrentRequest().getClientIp()"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVishwaKumarView Question on Stackoverflow
Solution 1 - SymfonymezeView Answer on Stackoverflow
Solution 2 - SymfonyKonstantin PelepelinView Answer on Stackoverflow
Solution 3 - SymfonyyvoyerView Answer on Stackoverflow
Solution 4 - SymfonyshacharsolView Answer on Stackoverflow
Solution 5 - SymfonyVolkerView Answer on Stackoverflow