php - How do I get the external IP address in a Symfony2 controller? -


similar https://stackoverflow.com/a/9029790/1069375

what symfony2 (built-in?) way external/public ip address of web server?

eg. there close this?

$this->container->get('request')->getclientip(); // returns string '::1' (length=3) 

note

i coding , testing on localhost, behind nat firewall. don't want "127.0.0.1" answer, whatever web visitors perceive should gateway forward www ports dev server. use dynamic dns hostname, in case have resort it, prefer more portable method.

to avoid going off topic in comments:

 $_server['remote_addr'] // returns '::1' 

you have 2 possibilities. first 1 simple one, ask third parties server:

$externalcontent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/current ip address: ([\[\]:.[0-9a-fa-f]+)</', $externalcontent, $m); $externalip = $m[1]; 

and other one, parse server config, like:

/sbin/ifconfig |grep -b1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "link" ) { printf "%s:" ,$1 } }' |awk -f: '{ print $1 ": " $3 }' 

you can call php using shell_exec


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -