PHP Exercises: Get the client IP address
PHP : Exercise-5 with Solution
Write a PHP script to get the client IP address.
IP address: An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing.
Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, because of the growth of the Internet and the depletion of available IPv4 addresses, a new version of IP (IPv6), using 128 bits for the IP address, was developed in 1995, and standardized in December 1998. In July 2017, a final definition of the protocol was published. IPv6 deployment has been ongoing since the mid-2000s.
IP addresses are usually written and displayed in human-readable notations, such as 172.16.254.2 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing prefix of the address is designated in CIDR notation by suffixing the address with the number of significant bits, e.g., 192.168.1.16/24, which is equivalent to the historically used subnet mask 255.255.255.0.
The IP address space is managed globally by the Internet Assigned Numbers Authority (IANA), and by five regional Internet registries. They are responsible in their designated territories for assignment to end users and local Internet registries, such as Internet service providers. IPv4 addresses have been distributed by IANA to the RIRs in blocks of approximately 16.8 million addresses each. Each ISP or private network administrator assigns an IP address to each device connected to its network.
Sample Solution:
PHP Code:
<?php
// Check if the IP is from shared internet
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
// Check if the IP is from a proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// If not from shared internet or proxy, consider it as the remote address
else {
$ip_address = $_SERVER['REMOTE_ADDR'];
}
// Display the determined IP address
echo $ip_address;
?>
Sample Output:
View the output in the browser
Explanation:
Here's a brief explanation of the above exercise:
- if (!empty($_SERVER['HTTP_CLIENT_IP'])) { ... }
- Checks if the IP address is present in the HTTP_CLIENT_IP server variable, which might indicate a shared internet connection. If present, assign the IP address to the variable '$ip_address'.
- elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { ... }
- Checks if the IP address is present in the HTTP_X_FORWARDED_FOR server variable, which might indicate a proxy. If present, assign the IP address to the variable '$ip_address'.
- else { ... }
- If the IP address is not found in the previous checks, it is considered the remote address. Assign the IP address to the variable '$ip_address'.
- echo $ip_address;
- Displays the determined IP address by echoing the value stored in the variable '$ip_address'.
Flowchart:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Create a simple HTML form and accept the user name and display the name through PHP echo statement.
Next: Write a simple PHP browser detection script.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php-exercises/php-basic-exercise-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics