Home  >  Article  >  Backend Development  >  How to Retrieve the Local IP Address of Your PHP Script?

How to Retrieve the Local IP Address of Your PHP Script?

Susan Sarandon
Susan SarandonOriginal
2024-10-25 00:51:30598browse

How to Retrieve the Local IP Address of Your PHP Script?

PHP Local IP Retrieval

In certain scenarios, it may be necessary to obtain the local IP address of a system running a PHP script. This address differs from the external IP, as it pertains specifically to the machine's local network card.

CLI Implementation

For PHP versions prior to 5.3.0, the following code can be used:

<code class="php">$localIP = getHostByName(php_uname('n'));</code>

For PHP versions 5.3.0 and above:

<code class="php">$localIP = getHostByName(getHostName());</code>

Note:

  • getHostName() returns the hostname of the system, while getHostByName() resolves the hostname to an IP address.
  • The code above assumes that the system can resolve its own hostname. If this is not the case, an alternative method may need to be employed, such as using ifconfig or ipconfig.

The above is the detailed content of How to Retrieve the Local IP Address of Your PHP Script?. 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