Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working with an API that requires the machine's external IP. As far as I know, the PHP environment I'm using can only get our internal IP.

The option on the table is using an external service such as to tell us:

wget -q -O -

My concern is what happens if that fails. Is there a bulletproof way of determining a machine's IP without relying on external services?

share|improve this question
The API needs a really good reason to throw away the valuable abstraction DNS offers. You might want to see if there are any alternatives. – hurfdurf Jan 13 '11 at 18:19
Yeah, it doesn't really make sense to me why they would even need the IP for a machine they probably don't have access to. We have all traffic go through a load balancer, but the virtual IP in front of that is likely not the one the machine will use for outbound requests. – editor Jan 13 '11 at 18:31
up vote 3 down vote accepted

No. Because NAT is supposed to be transparent that pertty much rules out doing anything you can do on the computer itself. You can bet (check IP addresses against defined private areas) but this is not bullet proof either.

share|improve this answer

This would be more of a question for StackOverflow, but I'll bite anyway! Presuming there's no Port Forwarding or load balancing involved, you can use the $_SERVER['SERVER_ADDR'] variable to get the ip that the script is running on.

Another gotcha is that it might not be the ip that any connections from the script are going out from. Take for example if you have a server with a primary ip of, and a secondary ip of for an SSL site on which the script is running. When you use the variable above, the ip returned will be, but when you create an outbound connection in the sciprt, it will be from unless you specifically bind to

share|improve this answer

because you say internal and external I assume that you use some sort of firewall that does nat for you. if this is the case is impossible to tell from your local script what ip you have, this is because once the network packets leave your box you can't tell what it will happen.

a simple solution is to use a few servers that are external (some free hosting) or have an external server that you call from php and returns you the ip address, as pointed out the array SERVER holds this info. if you have more then one server you can call the next and so on.

share|improve this answer

You could set up another server with a different ISP and write your own version of, running on hardware that you control.

share|improve this answer

Your best bet is to use more than one external source for your IP information. In addition to whatismyip most, if not all, DDNS providers (e.g. DynDNS) have a similar service. If you can't get the information from one site simply try the next one in the list.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.