2

I've written the following to get the user IP:

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

    if (!empty($server->get('HTTP_CLIENT_IP')))
    {
        $ip = $server->get('HTTP_CLIENT_IP');
    }
    else if (!empty($server->get('HTTP_X_FORWARDED_FOR')))
    {
        $ip = $server->get('HTTP_X_FORWARDED_FOR');
    }
    else
    {
        $ip = $server->get('REMOTE_ADDR');
    }

    return $ip;
}

I'd like to filter the value using the JFilterInput->clean() approach, using one of the options available, such as 'STRING', 'INT', etc.

I looked at INT, but don't think this will allow IP's with the following regex:

$pattern = '/[-+]?[0-9]+/';

as it doesn't accept a .(dot).


I then looked at CMD, and was wondering if this would be the best one to use as I believe it does allow a .(dot):

$pattern = '/[^A-Z0-9_\.-]/i';

Would it be best to use CMD? If not, which one? Or would be be best to use one of the native PHP filters?

2

For the time being, I've used PHP native filter_var with the FILTER_VALIDATE_IP filter, like so:

return filter_var($ip, FILTER_VALIDATE_IP);

I'm still of course open to other suggestions

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.