How to Get Local IP Address of System using PHP ? - GeeksforGeeks

02 Mar, 2020
IP (Internet Protocol) Address is an address of your network hardware. It helps in connecting your computer to other devices on your network and all over the world. An IP Address is made up of numbers or characters combined by ‘.’ symbol.
Example: It is an example of IP address.
506.457.14.512
All devices that are connected to an internet connection have a unique IP address which literally gives the idea that a large number of IP addresses had to be created for every system.
Local IP address: It is the IP address of your computer system which is meant to be kept private and thus is also known as private IP addresses. This article focuses on how to extract the IP address. A private IP address is the address of your device connected to the home or business network. If you have a few different devices connected to one ISP (Internet Service Provider), then all your devices will have a unique private IP address. This IP address cannot be accessed from devices outside your home or business network. Private IP addresses are not unique because there is a limited number of devices on your network. There are some other types of IP addresses that exist like, public IP addresses, static IP addresses and dynamic IP addresses that follow the same format for representation.
IPv4 address: The IPv4 version used to configure IP addresses in numerical value and it uses the hexadecimal number system to complete its job i.e. what makes it possible to get millions of IP, different for every system.
Example: In this example, we are trying to get the local IP address without using terminal and its commands. Instead, we are using a PHP program to get the same job done. We will be required two methods that are mentioned below:
getHostByName() Function: It gets the IPv4 address corresponding to a given Internet host name.
getHostName() Function: It gets the standard host name for the local machine.
Program: Here, first we get the name of the local machine as the string and then by using that we will get the corresponding address to that name.<?php  // Declaring a variable to hold the IP// address getHostName() gets the name// of the local machine getHostByName()// gets the corresponding IP$localIP = getHostByName(getHostName());  // Displaying the address echo $localIP;  ?>
<?php  // Declaring a variable to hold the IP// address getHostName() gets the name// of the local machine getHostByName()// gets the corresponding IP$localIP = getHostByName(getHostName());  // Displaying the address echo $localIP;  ?>
The fact that no output is provided with this program because of security reasons. Sharing of IP addresses can lead to security breaches and one should be careful not to share it. Any unethical access can cost you theft of personal space and even identity.
PHP-Misc
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to pass JavaScript variables to PHP ?
How to run JavaScript from PHP?
Download file from URL using PHP
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to run JavaScript from PHP?
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP