Determining the Actual IP Address in PHP
When attempting to determine the IP address of a machine using PHP, one may encounter a result of "::1". This arises due to your code utilizing the $_SERVER['REMOTE_ADDR'] variable, which returns the IPv6 loopback address.
The IPv6 loopback address, represented as "::1", signifies that the server is attempting to communicate with itself, akin to using "127.0.0.1" with IPv4. This indicates that the current network connection is through the localhost interface.
If you require a different IP address, such as the public IP address, you must establish a connection to the server via another network interface. This could involve utilizing a different Ethernet port or Wi-Fi adapter.
Once connected through the desired interface, you can employ the following code to retrieve the IP address:
<code class="php">echo $_SERVER['SERVER_ADDR'];</code>
This will output the IP address associated with the active network connection.
The above is the detailed content of How to Get the Real IP Address in PHP. For more information, please follow other related articles on the PHP Chinese website!