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
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:
$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():
$localIP = getHostByName(getHostName());
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3