I am facing an issue where I am unable to determine the client's IP address correctly.
I have gone through the following question How to get the client IP address in PHP? but this does not resolve my situation.
Requirement is as given below.
- If the visitor connects from IPv4 address, the script should show me the client's IPv4 address (This is handled in the script given below)
- If the visitor connects from IPv6 address, the script should show me the client's IPv6 address (The script given below converts the IPv6 address to IPv4 and does not give me the correct IPv6 address)
Given below is the script to capture the client's IP address.
<?php
function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
echo get_client_ip();
?>
Do I need to tweak PHP/Ubuntu/NGINX configuration or possibly A records?
Can anybody please point me out what am I missing to achieve this functionality? I am on PHP7, Ubuntu 16.04 and NGINX
Thanks