tags:

views:

397

answers:

5

Ok simple enough

<?PHP
echo $_SERVER[REMOTE_ADDR];
?>

Ok maybe not, I my IP is currently 72.184.212.85 however the code above which I am using on an IP blocking system for a script shows my IP as my home server IP of 127.0.0.1

So when I go to my script my IP is shown as 127.0.0.1 but when I go to other websites it is shown as 72.184.212.85

How can I get the first value to show on my test server?

+12  A: 

$_SERVER['REMOTE_ADDR'] will always show the IP address from which the request came. If you access your own script on your own computer within your own network, your external IP address never comes into play. The request would have to leave your local network and then come back in for the external address to show up, but if it's all local, that'll never happen.

deceze
Well, not *always*. From what I understand, if the connection comes through via a proxy, REMOTE_ADDR will contain the proxy's address rather than the IP of the actual caller. But the answer is solid, regardless.
Andrey Butov
"The IP from which the request came." If the request came from a proxy (which is doing the request on the users behalf), then it'll be the proxy's address. That's how TCP/IP works, PHP can't magically see behind proxies.
deceze
+1  A: 

You'll have to make your server publicly accessible and then access it from the public address. I'm guessing you're currently using localhost to access your server?

run your server say port 8080 and then forward the port in your router so it's public to the internet. Then visit your webpage/phpscript from http://72.184.212.85:8080 instead of http://localhost:8080.

Charles Ma
He better have a really good reason to publicly expose a port like this.
deceze
I wish I could get this to work, I tried using no-ip.org followed all the settings instructions but never could get my dev server to be on the net
jasondavis
while my site is still in dev. I don't have a host server yet so it would be nice to show friends and such progress made
jasondavis
That question seems better suited for http://serverfault.com :)
deceze
wow thank you I didnt know about that site
jasondavis
A: 

Here is a ridiculous solution that I wouldn't recommend:

Register your home IP with a domain name, then see where the request came from via URL:

$url = $_SERVER["SERVER_NAME"];

or

$url = $_SERVER["HTTP_HOST"];

and then do a dns lookup of that result, which should return the IP it's registered to, ie your external IP.

 $ext_ip = gethostbyaddr($url);

The only reason this wouldn't work (so sorry if I'm wrong), is if SERVER_NAME uses the same method as "REMOTE_HOST", which is a reverse DNS lookup, which won't resolve, as your internal IP won't be registered to that domain name. An easy way to check is to do either:

 phpinfo();

and see what the environmental variables are.

Anthony
I highly doubt this makes any difference if he's accessing his site via `localhost`.
deceze
A: 

HI, I have my home server and registered a free dns with dyndns.com and also when I type de domain name, I get the internal IP instead of the public IP. Anybody knows why?. That started to happen when I changed my belkin router for a Linksys router. With the Belkin, I always get the real ip (external)

Any suggestions will be appreciated.

Italo
A: 

because I am connected to the interent through a server .

Xilonard