6

I want to store the user's IP address in a Joomla 3.5 component; with 2.5, I did a

$ip = JRequest::getVar('REMOTE_ADDR', '', 'SERVER');

Which is the most efficient way to get the IP address from a client using the JInput class, since JRequest is deprecated?

1

2 Answers 2

9

Please check this code -

$jinput = JFactory::getApplication()->input;

$ip = $jinput->server->get('REMOTE_ADDR', '', '');

//Here default value and filter are null, so you can write 

$ip = $jinput->server->get('REMOTE_ADDR');

By this you can get the ip address.

3
  • I don't know, what the 3rd parameter 'SERVER' should do. It's a filter, but not a standard one; should we change this to 'CMD' or remove the 3rd parameter, because CMD is the default one? Or can I define a filter by myself in Joomla 3.x? Apr 4, 2016 at 14:39
  • Yes SERVER wasnt required and you can leave the last parameter as Blank.
    – Joomler
    Apr 5, 2016 at 7:07
  • 1
    I believe the default value and filter are automatically null, so if they're not going be defined by you, then you can replace ->get('REMOTE_ADDR', '', '') with ->get('REMOTE_ADDR')
    – Lodder
    Apr 5, 2016 at 22:37
2

I put a one-liner - and it works similar to your answer, because I do not use the object $jinput more than once; again thank you Joomler +1:

$ip = JFactory::getApplication()->input->server->get('REMOTE_ADDR','');

You could remove the second parameter too. If so, the $ip will be set to null if REMOTE_ADDR is not available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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