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

Susan Sarandon
Release: 2024-10-25 00:51:30
Original
665 people have browsed it

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>
Copy after login

For PHP versions 5.3.0 and above:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template