3

I am attempting to validate the user IP address in a post. Joomla 3.5 and PHP 5.6.20.

I have read How can I grab the user's IP address? and tried this out, but the wrong IP address is being returned.

Yes, my client is in a NAT but I checked the external port on the router for my external dhcp address, plus I compared against whatismyipaddress.com to ensure I am not (too) crazy.

Most likely, this is due to a proxied setup by my hosting provider (they are now using CloudFlare). Any ideas on how to resolve the client ip address in this situation?

  • I verified that the address being returned by the posted code (see question title) is indeed owned by CloudFlareNet. – Tim Apr 24 '16 at 13:53
  • 1
    Maybe bad form to answer my own post, but I did locate the answer. In my situation, the following line: $clientip = JFactory::getApplication()->input->server->get('HTTP_X_FORWARDED_FOR',''); returns the correct value when the server is proxied! – Tim Apr 24 '16 at 14:20
  • You can post your comment-answer as an answer below and mark it as the one that solved your issue. – FFrewin Apr 24 '16 at 14:30
3

Maybe bad form to answer my own post, but I did locate the answer once I moved beyond the Joomla documentation.

In my situation, the following line:

$clientip = JFactory::getApplication()->input->server->get('HTTP_X_FORWARDED_FOR',''); 

returns the correct value for the user ip address when the server is proxied!

  • Feel free to answer your own questions, it's not bad form. Regarding your answer, be aware that HTTP_X_FORWARDED_FOR can be easily spoofed. Unfortunately, I don't know a way around this. – TryHarder Apr 24 '16 at 23:54
1

Just an FYI, there's no way to always get the real IP address as it may be proxied.

Based on the following answer on Stack Overflow, you could use something like this:

function getRealIpAddr()
{
    $server = JFactory::getApplication()->input->server;

    if (!empty($server->get('HTTP_CLIENT_IP')))    //check ip from share internet
    {
        $ip = $server->get('HTTP_CLIENT_IP');
    }
    elseif (!empty($server->get('HTTP_X_FORWARDED_FOR')))  //to check ip is pass from proxy
    {
        $ip = $server->get('HTTP_X_FORWARDED_FOR');
    }
    else
    {
        $ip = $server->get('REMOTE_ADDR');
    }

    return $ip;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.