Understanding the Local IP Address and Retrieving it in PHP
In PHP, when attempting to fetch the IP address of a machine, you may encounter the result "::1," which represents the IPv6 loopback address (localhost). However, you may be unsure why this address is being displayed instead of an expected IPv4 address.
The reason for this is that "::1" is the actual IP address for the loopback interface. This interface is a virtual network interface that connects the machine to itself and is used for local communication. In IPv4, the loopback address is "127.0.0.1."
If you wish to obtain a different IP address, you will need to connect to the server through a different network interface. This can be achieved by using a physical network adapter, such as an Ethernet or Wi-Fi interface, or a virtual network adapter, such as a VPN or software-defined networking (SDN) interface.
To retrieve the actual IP address of the machine, you can use the following code:
<code class="php">echo $_SERVER['SERVER_ADDR'];</code>
This code will display the primary IP address associated with the server, typically the IP address of the physical or virtual network adapter that is active and connected to a network.
The above is the detailed content of Why is the IPv6 Loopback Address (::1) Displayed Instead of the IP Address in PHP?. For more information, please follow other related articles on the PHP Chinese website!