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

VishwaKumar picture VishwaKumar · Jan 27, 2012 · Viewed 77.2k times · Source

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?

Answer

meze picture meze · Jan 27, 2012

You can get the client IP using Request service:

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