2

Similar to https://stackoverflow.com/a/9029790/1069375

What is the most Symfony2 (built-in?) way to get the external/public IP address of the web server?

Eg. is there anything close to this?

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

NOTE

I am coding and testing on localhost, behind a NAT firewall. I don't want "127.0.0.1" as an answer, but whatever web visitors would perceive should the gateway forward the www ports to my dev server. I already do use a dynamic DNS hostname, in case I have to resort to it, but prefer a more portable method.

To help avoid going off topic in the comments:

 $_SERVER['REMOTE_ADDR'] // also returns '::1'
Community
  • 1
  • 1
Marcos
  • 4,796
  • 5
  • 40
  • 64
  • Excuse me, naysayer, care to offer a helpful comment first?? – Marcos Mar 24 '14 at 11:41
  • 2
    getClientIp() should be working as you have it. But mind, if you test locally using http://localhost/, the ip address will be 127.0.0.1. The connection stays within your pc and does not enter the internet. You should access your pc through the internt (you should port forward in your NAT and then visiting your public ip address). – Veda Mar 24 '14 at 11:46
  • I am specifically asking how to return the external IP address, regardless who's visiting. Not 127.0.0.1, and not what getClientIp() does. – Marcos Mar 24 '14 at 11:51
  • Even Symphony can only return data from $_SERVER["REMOTE_ADDR"]. This is enough in almost all cases. If your not behind some Loadbalancer or complex firewall, all is fine. Just check as @Veda suggested. – ToBe Mar 24 '14 at 12:01
  • I realize that, yes. Would you recommend I rephrase my question to already suggest an answer(actually, workaround) on how `in code` to quickly read from **whatsmyip.org**? The point is trying not to rely on 3rd parties, however. – Marcos Mar 24 '14 at 12:11
  • @Marcos that was not clear to me. PHP does not know this by default. You can do what you as you stated in youe question and resolv dyndns, or do a curl call to a script like this using your domain (I use this trick myself and hosted the script on some shared webhosting provider) or as you commented already, get the data from whatsmyip.org. – Veda Mar 24 '14 at 12:13
  • When testing this locally with a local webserver your *global* IP becomes `127.0.0.1` because the traffic wont leave your internal network. – ferdynator Mar 24 '14 at 12:49
  • Possible duplicate question: http://stackoverflow.com/questions/177414/get-my-wan-ip-address. Indicates why it may not be as easy as it first appears to get your external ip address. – Ryan Vincent Mar 24 '14 at 13:33
  • @RyanVincent Simple, because this is a Symfony question (looking for an in-framework solution, if possible). – Marcos Mar 24 '14 at 13:43

3 Answers3

2

You have two possibilities. The first one is the simple one, ask some 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 the other one, is to parse your 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 it from php using shell_exec

pomaxa
  • 1,740
  • 16
  • 26
1

You said you do have a dynamic host name. Use PHP inbuilt function:

$serverIp = gethostbyname('your_subdomain.dyndns.org');

This should read your external IP address from your locally configured DNS.

Update:

A properly configured server contains the correct information in:

echo $_SERVER['SERVER_ADDR'];
echo $_SERVER['SERVER_NAME'];
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

I hate to answer my own question, and will gladly mark a better answer (short and elegant and independent of outside servers unlike below, maybe using the network stack, traceroute or some such wizardry), but for now for a task this small we needed something quick for staging or production, so:

  $CustomerIp =  system("curl -s ipv4.icanhazip.com") ;  // returns string '93.9.29.389' (length=11)
Marcos
  • 4,796
  • 5
  • 40
  • 64