-2

Hmm, $_SERVER['REMOTE_ADDR'] and WhatIsMyIP show me two different IP addresses. I want to catch the IP address like how it appears on "whatismyip".

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mac Umatik
  • 63
  • 1
  • 9
  • In what situation exactly? Are you testing on your own localhost? – deceze Feb 07 '13 at 13:37
  • Can we see some of the code you've used? `$_SERVER["REMOTE_ADDR"]` should give the same result, unless you contact a server on an internal network? – h2ooooooo Feb 07 '13 at 13:38
  • Check the [answer here](http://stackoverflow.com/questions/4686318/getting-users-real-ip-address-using-php?rq=1) – hd1 Feb 07 '13 at 13:38
  • 1
    Are you behind a router? If you're NATed, there's no way to determine your real IP address from inside the NAT, I don't beleive. At least I wouldn't imagine there's any way for PHP to do it. I believe the only way is to find out from something external, like whatismyip.com. You might be able to hit that page and parse the HTML to get it. – Pete Feb 07 '13 at 13:38
  • If you are seeing a 192.168.x.x (assuming normal home address scheme) then you are seeing your local address whereas whatismyip is showing your global (from ISP) address – UnholyRanger Feb 07 '13 at 13:38
  • possible duplicate of [My IP is showing up wrong in PHP home server](http://stackoverflow.com/questions/1181189/my-ip-is-showing-up-wrong-in-php-home-server) – deceze Feb 07 '13 at 13:39
  • $_SERVER['REMOTE_ADDR'] shows me 10.127.254.21, but my real ip addres is 85.89.XXX.XXX. And I'm behind router .. – Mac Umatik Feb 07 '13 at 14:00
  • @Mac That *is* the ***real*** IP. It's the IP that was used in the login process. A machine may have more than one IP address. IP addresses are just addressing mechanisms for delivering data packets over a network. There's no "wrong" IP, if it was used to deliver the data it is the *right* IP. You will get different results once you put the application on a server on a public network, because then the data routing will be different. – deceze Feb 07 '13 at 15:44

3 Answers3

3

Some networking 101:

    +--+
    |  |
    +--+
whatismyip.com                 +--+
108.162.207.135         +----- |  |
                        |      +--+
     |       +--+       |  your computer
     +------ |  | ------+  10.127.254.21
             +--+       |
          NAT router    |      +--+
          85.89.XX.XX   +----- |  |
                               +--+
                          your web server
                           10.127.254.22

If this is your network setup, your web server will see 10.127.254.21 as the IP of your computer when the two communicate. That's because to route data from your computer to your web server, the packets just need to travel directly on the wire between the two. If the two are the same machine, it doesn't even need to do that.

whatismyip.com will see 85.89.XX.XX as the originating IP address if you try to access it from your computer, since the data traverses your NAT router on the way from your computer to whatismyip.com. The NAT router proxies the request using NAT technology, hiding your actual IP address in the process. To whatismyip.com it will look like the request came from the router, not your your computer.

Wanting to get 85.89.XX.XX as an answer from $_SERVER['REMOTE_ADDR'] on your web server is rather arbitrary. It's not "your real IP", it's the IP of the router which doesn't really have anything to do with the communication between your computer and your web server.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • no ... I use external server. And I have to know orginal IP addresss of login users – Mac Umatik Feb 09 '13 at 11:27
  • -_-;; What exactly does that mean, "external server"? What does your network topology look like? The bottom line is though: you get what's in `$_SERVER['REMOTE_ADDR']`. That's the IP from which the request came. This may or may not be the "actual" address of the machine (e.g. router/proxy address vs. local address), but it's the only one you'll get. If you have some specific situation which does not fit this answer, describe your situation in more detail. – deceze Feb 09 '13 at 11:49
  • heh ... this is typical sytuation. I work on remonte server and, I have to collect information of login users: login time, loginame and IP address for security reason, so internal IP address is useless for me. – Mac Umatik Feb 09 '13 at 13:14
1

The site is showing your global ip address (the one you use to connect to internet). REMOTE_ADDR shows the client address, wich i'm guessing is your own machine, not going through internet at all. That's why it's showing you an local ip address, instead of a global one.

Bottomline: You request the remote server (whatismyip.com) with your global address, but request your local server with your local address (127.0.0.1 if it's on the same machine).

Hugo Mota
  • 11,200
  • 9
  • 42
  • 60
0

When you're running your local web server and you use $_SERVER['REMOTE_ADDR'] it will return your address on the local network (if on a different computer) or 127.0.0.1 (A.K.A. localhost) if on your own computer. Whereas "What Is My IP" will look at your network's IP address to the Internet. I hope this helps to clarify what's happening.

Regardless, $_SERVER['REMOTE_ADDR'] should work fine when running "live" on a proper web server, so if that's what you're planning to do, you shouldn't need to worry.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • ok I see, but there is any chance to catch global IP address via php ? – Mac Umatik Feb 07 '13 at 14:12
  • @MacUmatik If you forward your ports and then try to connect (or get something else to connect) to your web server through your network's "external" IP address; then yes. – EM-Creations Feb 07 '13 at 15:00
  • hmm ... maybe should ask a different question... I need to create an array with login time, user name, and ip address, and put it into database, so using $_SERVER['REMOTE_ADDR'] is useless, because it show me internal IP address. So how can I get info of real user IP ? – Mac Umatik Feb 07 '13 at 15:21
  • @MacUmatik If someone else were to connect to your web server it wouldn't show up as 127.0.0.1 for them. It only shows up as 127.0.0.1 for you as your computer is running the web server. If you put the code on a production web server, when it's ready it would not be a problem. – EM-Creations Feb 07 '13 at 15:54
  • @MacUmatik I hope it's helped. :) – EM-Creations Feb 07 '13 at 17:18