Home  >  Article  >  Backend Development  >  How to Get the Real IP Address in PHP

How to Get the Real IP Address in PHP

Linda Hamilton
Linda HamiltonOriginal
2024-10-22 22:07:29141browse

How to Get the Real IP Address in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn