1
$_server['REMOTE_ADDR'];

This code is used to get user's IP. If I'm on localhost it gives 127.0.0.1 but my IP is not this and if I go to live web page the IP Address is another. I'm the user then why IP 127.0.0.1?

Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47

2 Answers2

2

You said:

If I am on localhost it gives 12.0.0.1

That's correct because that is the address for localhost. See here if you'd like to know why it has that address.

From the wikipedia entry on localhost:

In computer networking, localhost means this computer. It is a hostname that the computer's software and users may employ to access the computer's own network services via its loopback network interface. Using the loopback interface bypasses local network interface hardware.

I hope that helps.

2

You've installed a web server on your local machine.

This is required because, while development you need to test your development & while doing this, you need to emulate the server on which your project will launch.

Server is required to server the request of every client & so to emulate it on local machine we need to assign it to some ip address.

Now, Why 127.0.0.1 ?

@Jhon T 127 is the last network number in a class A network with a subnet mask of 255.0.0.0. 127.0.0.1 is the first assignable address in the subnet. 127.0.0.0 cannot be used because that would be the wire number. But using any other numbers for the host portion should work fine and revert to using 127.0.0.1. You can try it yourself by pinging 127.1.1.1 if you'd like. Why they waited until the last network number to implement this? I don't think it's documented

For More Clarity

@Captain Pedantic 127.0.0.1 is normally the IP address assigned to the "loopback" or local-only interface. This is a "fake" network adapter that can only communicate within the same host. It's often used when you want a network-capable application to only serve clients on the same host. A process that is listening on 127.0.0.1 for connections will only receive local connections on that socket.

"localhost" is normally the hostname for the 127.0.0.1 IP address. It's usually set in /etc/hosts (or the Windows equivalent named "hosts" somewhere under %WINDIR%). You can use it just like any other hostname - try "ping localhost" to see how it resolves to 127.0.0.1.

0.0.0.0 has a couple of different meanings, but in this context, when a server is told to listen on 0.0.0.0 that means "listen on every available network interface". The loopback adapter with IP address 127.0.0.1 from the perspective of the server process looks just like any other network adapter on the machine, so a server told to listen on 0.0.0.0 will accept connections on that interface too.

Reference

https://superuser.com/questions/31824/why-is-localhost-ip-127-0-0-1

What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

Community
  • 1
  • 1
Abhineet Verma
  • 1,008
  • 8
  • 18