Grabbing the user's IP address

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
User avatar
cparekh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Mon Jan 14, 2008 10:48 am
Location: London, UK
Contact:

Grabbing the user's IP address

Post by cparekh » Thu Jan 08, 2009 3:27 pm

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.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17427
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Grabbing the user's IP address

Post by toivo » Thu Jan 08, 2009 8:22 pm

You can use the following to get the IP address:

Code: Select all

	$ip = $_SERVER['REMOTE_ADDR'];
Regards,
Toivo Talikka, Global Moderator

User avatar
cparekh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Mon Jan 14, 2008 10:48 am
Location: London, UK
Contact:

Re: Grabbing the user's IP address

Post by cparekh » Fri Jan 09, 2009 8:33 am

Hi Toivo,

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

Thanks again,
C.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: Grabbing the user's IP address

Post by dam-man » Fri Jan 09, 2009 2:15 pm

Joomla doesn't have a specific function for this.
Just use this: $ip = $_SERVER['REMOTE_ADDR']; like toivo said
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

fluffybunny
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jul 10, 2009 4:30 pm

Re: Grabbing the user's IP address

Post by fluffybunny » Thu Dec 24, 2009 7:26 am

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

User avatar
cparekh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Mon Jan 14, 2008 10:48 am
Location: London, UK
Contact:

Re: Grabbing the user's IP address

Post by cparekh » Thu Dec 24, 2009 8:58 am

You can use this code anywhere in your php script. I wrote a custom php module and used it there.

Hth,
C.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com

fluffybunny
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jul 10, 2009 4:30 pm

Re: Grabbing the user's IP address

Post by fluffybunny » Thu Dec 24, 2009 9:38 am

@cparekh

Thanks for your quick reply.

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

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Grabbing the user's IP address

Post by ianmac » Thu Dec 24, 2009 4:25 pm

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

AmyStephen
Joomla! Champion
Joomla! Champion
Posts: 7018
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Grabbing the user's IP address

Post by AmyStephen » Thu Dec 24, 2009 4:48 pm

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

fluffybunny
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jul 10, 2009 4:30 pm

Re: Grabbing the user's IP address

Post by fluffybunny » Thu Dec 24, 2009 6:15 pm

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)

AmyStephen
Joomla! Champion
Joomla! Champion
Posts: 7018
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Grabbing the user's IP address

Post by AmyStephen » Thu Dec 24, 2009 7:36 pm

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

fluffybunny
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jul 10, 2009 4:30 pm

Re: Grabbing the user's IP address

Post by fluffybunny » Sun Dec 27, 2009 8:52 am

@ 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.

AmyStephen
Joomla! Champion
Joomla! Champion
Posts: 7018
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Grabbing the user's IP address

Post by AmyStephen » Sun Dec 27, 2009 3:12 pm

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!

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Grabbing the user's IP address

Post by ianmac » Mon Dec 28, 2009 7:45 am

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

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Grabbing the user's IP address

Post by ianmac » Mon Dec 28, 2009 7:47 am

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

fluffybunny
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jul 10, 2009 4:30 pm

Re: Grabbing the user's IP address

Post by fluffybunny » Mon Dec 28, 2009 8:23 am

@ianmac

Sorry about that. 8)

AmyStephen
Joomla! Champion
Joomla! Champion
Posts: 7018
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: Grabbing the user's IP address

Post by AmyStephen » Mon Dec 28, 2009 3:18 pm

Ian - thanks for that, very helpful - apologize for taking this off-topic.

User avatar
cparekh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 120
Joined: Mon Jan 14, 2008 10:48 am
Location: London, UK
Contact:

Re: Grabbing the user's IP address

Post by cparekh » Thu Jan 14, 2010 3:11 pm

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.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com

User avatar
RustyJoomla
Joomla! Explorer
Joomla! Explorer
Posts: 409
Joined: Wed Aug 01, 2007 12:08 am
Location: UK
Contact:

Re: Grabbing the user's IP address

Post by RustyJoomla » Wed Aug 14, 2013 2:15 pm

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
Follow me on Twitter - http://twitter.com/RustyJoomla
Company Joomla Website - https://storm.agency


Locked

Return to “Joomla! 1.5 Coding”