Obtaining the Local IP Address of a System Using PHP
Determining the local IP address of a computer is crucial in various networking scenarios. PHP provides built-in functions that enable you to retrieve this information.
Getting the local IP address is particularly useful when you need to know the specific network interface address of the system running your PHP script. This address is distinct from the external IP address that is accessible to the outside world.
PHP Solutions Based on PHP Version
PHP offers different approaches to obtaining the local IP address, depending on the version you are using:
PHP < 5.3.0
For PHP versions prior to 5.3.0, you can use the getHostByName() function, which takes the hostname as an argument. Here's how you can retrieve the local IP:
<code class="php">$localIP = getHostByName(php_uname('n'));
PHP >= 5.3.0
In PHP versions 5.3.0 and above, you have the option to use the getHostName() function to get the hostname, followed by getHostByName():
<code class="php">$localIP = getHostByName(getHostName());</code>
The value of $localIP will be the local IP address of the system. It will typically have the format "192.*...." as specified in the question.
The above is the detailed content of How to Determine the Local IP Address of a System in PHP?. For more information, please follow other related articles on the PHP Chinese website!