Page 1 of 1

Grabbing the user's IP address

Posted: Thu Jan 08, 2009 3:27 pm
by cparekh
Hi,

(firstly, Happy New Year to everyone)

I need to capture the user's IP address and save it to a db table I've set up. What existing Joomla function can I use to to do that? I've checked the api but not sure what I need to use.

Thanks in advance for any guidance.

Regards,
C.

Re: Grabbing the user's IP address

Posted: Thu Jan 08, 2009 8:22 pm
by toivo
You can use the following to get the IP address:

Code: Select all

	$ip = $_SERVER['REMOTE_ADDR'];
Regards,

Re: Grabbing the user's IP address

Posted: Fri Jan 09, 2009 8:33 am
by cparekh
Hi Toivo,

thanx for that. I thought Joomla would have it's own function but if not I'll use $SERVER[] :)

Thanks again,
C.

Re: Grabbing the user's IP address

Posted: Fri Jan 09, 2009 2:15 pm
by dam-man
Joomla doesn't have a specific function for this.
Just use this: $ip = $_SERVER['REMOTE_ADDR']; like toivo said

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 7:26 am
by fluffybunny
Sorry for my newbie question, but where do I need to put the above code? Because I want to capture a user's IP address as well.

Thanks in advance

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 8:58 am
by cparekh
You can use this code anywhere in your php script. I wrote a custom php module and used it there.

Hth,
C.

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 9:38 am
by fluffybunny
@cparekh

Thanks for your quick reply.

Any chance I could use your custom php module? :)

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 4:25 pm
by ianmac
fluffybunny wrote:@cparekh

Thanks for your quick reply.

Any chance I could use your custom php module? :)
You could also try:

echo JFactory::getSession()->get('session.client.address');

Ian

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 4:48 pm
by AmyStephen
Ian -

I've found myself wondering why the framework duplicates what is easily available using standard PHP. What are the advantages, for example, for Joomla! to have it's own session handling and substitutes for Super Globals? Is this related to 1.5 supporting PHP 4 and PHP 5?

It's just a curiosity that has been growing in me the more I work with J! and PHP development.

Thanks!
Amy

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 6:15 pm
by fluffybunny
You could also try:

echo JFactory::getSession()->get('session.client.address');
Thanks for the idea Ian.

I've looked at the factory.php file, but can't quite make out where I could put it. Should I add it with the 'GetUser' section? And if so, where exactly? (sorry, but am a real newbie with php, trying though)

function &getUser($id = null)
{
jimport('joomla.user.user');

if(is_null($id))
{
$session =& JFactory::getSession();
$instance =& $session->get('user');
if (!is_a($instance, 'JUser')) {
$instance =& JUser::getInstance();
}
}
else
{
$instance =& JUser::getInstance($id);
}

return $instance;
}



Oh, and merry Christmas everyone! 8)

Re: Grabbing the user's IP address

Posted: Thu Dec 24, 2009 7:36 pm
by AmyStephen
fluffybunny -

Are you creating an Extension? If so, add the code right inside of your Extension. It's never a good idea to change the core files, and seldom, if ever needed.

Merry Christmas to you, too!
Amy

Re: Grabbing the user's IP address

Posted: Sun Dec 27, 2009 8:52 am
by fluffybunny
@ Amy

No I'm not creating an extension, but ianMac suggested to add a line of code in the factory.php file as a solution for getting the user's IP addresses.

However, in the meantime, I have found another solution, albeit it not a solution for everybody with this problem. Turns out that the quiz module JQuarks also tracks IP addresses of those that do the quiz. For us, this is important since we want to prevent fraud from our members.

So you could just ask members 2 or 3 questions before giving them something that tempts fraudulous behaviour. Thanks to JQuarks, you can than see if they've tried getting it before.

I still think Joomla should add viewing the user's IP address in their core though, since there are many reasons why webmasters would want to be able to see where their user's are coming from, especially these days.

Just my 2cents. :)

Thanks all for helping.

Re: Grabbing the user's IP address

Posted: Sun Dec 27, 2009 3:12 pm
by AmyStephen
toivo wrote:

Code: Select all

<?php echo $_SERVER['REMOTE_ADDR']; ?>
ianmac wrote:

Code: Select all

<?php echo JFactory::getSession()->get('session.client.address'); ?>
If you copy either of those statements into your Templates's index.php file, the visitor's IP address will be displayed when they visit. Hacking core files is not recommended.

Happy new year!

Re: Grabbing the user's IP address

Posted: Mon Dec 28, 2009 7:45 am
by ianmac
AmyStephen wrote:Ian -

I've found myself wondering why the framework duplicates what is easily available using standard PHP. What are the advantages, for example, for Joomla! to have it's own session handling and substitutes for Super Globals? Is this related to 1.5 supporting PHP 4 and PHP 5?

It's just a curiosity that has been growing in me the more I work with J! and PHP development.

Thanks!
Amy
Joomla does use PHP's native session handling calls. If you look at JSession you will see this. It does wrap the native calls in its own class so that it is easy to extend and modify. This is useful, for example, when you are trying to load balance. If you use PHP's file session storage and you are load balancing across servers then you will not have the same sessions on every server. Saving sessions in the database solves this. It also allows you to use other session storage handlers really easily. If you look in lib/j/session/storage you will see some of the storage adapters that we have.

In terms of super globals, I assume you are talking about $_REQUEST, $_GET, $_POST and $_COOKIES. We wrap these because in our wrapper functions we provide filtering shortcuts that make it easier to do basic variable sanity checks and type enforcement. It is actually common to remove access to these all together in some frameworks and only allow access through special methods in order to enforce some semblance of third party code quality. This idea has been discussed in the past for Joomla.

Ian

Re: Grabbing the user's IP address

Posted: Mon Dec 28, 2009 7:47 am
by ianmac
fluffybunny wrote:@ Amy

No I'm not creating an extension, but ianMac suggested to add a line of code in the factory.php file as a solution for getting the user's IP addresses.
Modifying the factory.php file is not something that I would have suggested and I don't recall ever having done so. I merely gave you a snippet of code that you might use in your module or somewhere to output the user's IP address. I think you read my post incorrectly.

Ian

Re: Grabbing the user's IP address

Posted: Mon Dec 28, 2009 8:23 am
by fluffybunny
@ianmac

Sorry about that. 8)

Re: Grabbing the user's IP address

Posted: Mon Dec 28, 2009 3:18 pm
by AmyStephen
Ian - thanks for that, very helpful - apologize for taking this off-topic.

Re: Grabbing the user's IP address

Posted: Thu Jan 14, 2010 3:11 pm
by cparekh
fluffybunny,

sorry lost focus on this over the holiday period.

To capture a user's ip, you can use a function like this in a custom php module - I use the one from Fiji Web Design (http://www.fijiwebdesign.com/portfolio/ ... d_php.html)

Code: Select all

function getUserIP()
				{
				$ip = "";
				
				if (isset($_SERVER))
				{
				if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
				} elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$ip = $_SERVER["HTTP_CLIENT_IP"];
				} else {
				$ip = $_SERVER["REMOTE_ADDR"];
				}
				}
				else {
				if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
				$ip = getenv( 'HTTP_X_FORWARDED_FOR' );
				} elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {
				$ip = getenv( 'HTTP_CLIENT_IP' );
				} else {
				$ip = getenv( 'REMOTE_ADDR' );
				}
				}
				return $ip;
				} 
				
		$memberip = getUserIP();
hth,
C.

Re: Grabbing the user's IP address

Posted: Wed Aug 14, 2013 2:15 pm
by RustyJoomla
If I wanted to grab the user's local machine IP address, rather than their routers IP Address, can that be done also? For example, the Article Vote uses "lastip" which in turn uses $_SERVER['REMOTE_ADDR']; could this be set to use their local machine's IP?

This can be a problem if all users are in the same building, they are essentially using the same 'REMOTE_ADDR' so only 1 vote can be counted...

Thanks