views:

211

answers:

8

PHP gives a different IP address for my site from $_SERVER['SERVER_ADDR'] than other sites see when I access them through cURL (or file_get_contents, or whatever). I know this because I had my site download whatismyip.com and it gives something different than that server variable.

Adding to this annoyance is the fact that the outgoing IP seems to vary on a day-to-day basis. It might be more frequent than that, not completely sure. This is very frustrating indeed, because I'm utilizing an API that relies on hash's generated using the sending IP. So I can't even hardcode the outgoing one to use the API.

Why are these IP's different, and is there a way to dynamically get the one that other sites see me as when I open sockets with them? Or a possible work around?

Background: I'm on shared hosting with Godaddy.

+1  A: 

You might be hosted on a virtual server in a cluster. The physical server you are hosted on will have its own IP address on their local network and is accessed through another gateway server.

Do a var_dump($_SERVER) and see if there is any other information in that array that could be useful.

Petah
the outgoing IP does not appear in the dump
babonk
Then you might be short on luck. Unless you have a registered domain name, which you could DNS look up.
Petah
A: 

You could try using gethostbyname()

Petah
Gives the "ping" ip instead of the outgoing one.
babonk
+1  A: 

If they are doing NATing or something similar, you will not be able to know the real IP that your request will be assigned when it leaves the network to reach a remote server.

You have a chance if you can host another PHP page on a remote server. You can a write a very simple PHP page that returns the request IP address to the client requesting it.

For example:

echo $_SERVER['REMOTE_ADDR'];

Also, you can try to use:

$_SERVER['HTTP_X_FORWARDED_FOR']

in case your request is being proxied.

Khaled
This information relies the information provided by the client requesting information from the server, even then it is unreliable. Given the case outlined by the op, he would need to ask for this information on the host that the API is running on, not his own server. I'd be quite possible to assume that this is impossible.
Kevin Peno
It is just a suggestion to help.
Khaled
A: 

Not sure how hosting works on GoDaddy, but if you can use exec or system then you could run ifconfig (exec("ifconfig")).

Andrei Serdeliuc
+3  A: 

There seams to be an option to get a static IP at Godaddy: http://help.godaddy.com/article/1053

The only other solution I can think of, is determine the range in which Godaddy's proxy has its IP adresses and use the range, instead of the IP in your API.

JochenJung
I'd agree on this. If you don't actually have a statically assigned IP, then you are theoretically sharing and will never get consistent results. From the sound of your question, you need this additional service.
Kevin Peno
Actually I tried this and it DOESNT WORK. It just gives a static incoming IP, not a static outgoing one. Going with one of the hacks below
babonk
+2  A: 

Unless GoDaddy deliberately adds this information somewhere (I doubt) you can't discover it from PHP alone, and if NAT is doing its job properly, you couldn't even figure it out on network level.

If GoDaddy's routing is set up to always send data "outside", then this might work:

tell_ip.php:

echo $_SERVER['REMOTE_ADDR'];

main_script.php:

$my_external_ip = file_get_contents("http://" .
                     $_SERVER['HTTP_HOST'] . "/tell_ip.php");

However, you may get internal IP if their router is smart enough to route connection through LAN. In such case you need to put tell_ip.php script on some other server.

porneL
A: 

You could consider using a proxy that has a static IP and let it route to your service. Maybe GoDaddy got one of these in their network even? Then you'll get a consistent IP when calling your service.

Knubo
+1  A: 

This is a bit hackish, but you could have some page which merely outputs the receiving IP (echo $_SERVER[ "REMOTE_ADDR" ];) and then use file_get_contents( "http://your-site.whatever/path/to/page" ). This should output what you're looking for.

I've only done very preliminary tests for this, but it looks like it works.

Christopher W. Allen-Poole