Grabbing the user's IP address
- cparekh
- Joomla! Enthusiast
- Posts: 120
- Joined: Mon Jan 14, 2008 10:48 am
- Location: London, UK
- Contact:
Grabbing the user's IP address
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.
(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
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
- toivo
- Joomla! Master
- Posts: 17443
- Joined: Thu Feb 15, 2007 5:48 am
- Location: Sydney, Australia
Re: Grabbing the user's IP address
You can use the following to get the IP address:
Regards,
Code: Select all
$ip = $_SERVER['REMOTE_ADDR'];
Toivo Talikka, Global Moderator
- cparekh
- Joomla! Enthusiast
- Posts: 120
- Joined: Mon Jan 14, 2008 10:48 am
- Location: London, UK
- Contact:
Re: Grabbing the user's IP address
Hi Toivo,
thanx for that. I thought Joomla would have it's own function but if not I'll use $SERVER[]
Thanks again,
C.
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
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
- dam-man
- Joomla! Exemplar
- Posts: 7961
- Joined: Fri Sep 09, 2005 2:13 pm
- Location: The Netherlands
- Contact:
Re: Grabbing the user's IP address
Joomla doesn't have a specific function for this.
Just use this: $ip = $_SERVER['REMOTE_ADDR']; like toivo said
Just use this: $ip = $_SERVER['REMOTE_ADDR']; like toivo said
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards
Dutch Boards | Joomla Coding Boards | English Support Boards
-
- Joomla! Apprentice
- Posts: 15
- Joined: Fri Jul 10, 2009 4:30 pm
Re: Grabbing the user's IP address
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
Thanks in advance
- cparekh
- Joomla! Enthusiast
- Posts: 120
- Joined: Mon Jan 14, 2008 10:48 am
- Location: London, UK
- Contact:
Re: Grabbing the user's IP address
You can use this code anywhere in your php script. I wrote a custom php module and used it there.
Hth,
C.
Hth,
C.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
-
- Joomla! Apprentice
- Posts: 15
- Joined: Fri Jul 10, 2009 4:30 pm
Re: Grabbing the user's IP address
@cparekh
Thanks for your quick reply.
Any chance I could use your custom php module?
Thanks for your quick reply.
Any chance I could use your custom php module?
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: Grabbing the user's IP address
You could also try:fluffybunny wrote:@cparekh
Thanks for your quick reply.
Any chance I could use your custom php module?
echo JFactory::getSession()->get('session.client.address');
Ian
-
- Joomla! Champion
- Posts: 7018
- Joined: Wed Nov 22, 2006 3:35 pm
- Location: Nebraska
- Contact:
Re: Grabbing the user's IP address
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
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! Apprentice
- Posts: 15
- Joined: Fri Jul 10, 2009 4:30 pm
Re: Grabbing the user's IP address
Thanks for the idea Ian.You could also try:
echo JFactory::getSession()->get('session.client.address');
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!
-
- Joomla! Champion
- Posts: 7018
- Joined: Wed Nov 22, 2006 3:35 pm
- Location: Nebraska
- Contact:
Re: Grabbing the user's IP address
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
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
-
- Joomla! Apprentice
- Posts: 15
- Joined: Fri Jul 10, 2009 4:30 pm
Re: Grabbing the user's IP address
@ 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.
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.
-
- Joomla! Champion
- Posts: 7018
- Joined: Wed Nov 22, 2006 3:35 pm
- Location: Nebraska
- Contact:
Re: Grabbing the user's IP address
toivo wrote:Code: Select all
<?php echo $_SERVER['REMOTE_ADDR']; ?>
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.ianmac wrote:Code: Select all
<?php echo JFactory::getSession()->get('session.client.address'); ?>
Happy new year!
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: Grabbing the user's IP address
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.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
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
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: Grabbing the user's IP address
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.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.
Ian
-
- Joomla! Apprentice
- Posts: 15
- Joined: Fri Jul 10, 2009 4:30 pm
Re: Grabbing the user's IP address
@ianmac
Sorry about that.
Sorry about that.
-
- Joomla! Champion
- Posts: 7018
- Joined: Wed Nov 22, 2006 3:35 pm
- Location: Nebraska
- Contact:
Re: Grabbing the user's IP address
Ian - thanks for that, very helpful - apologize for taking this off-topic.
- cparekh
- Joomla! Enthusiast
- Posts: 120
- Joined: Mon Jan 14, 2008 10:48 am
- Location: London, UK
- Contact:
Re: Grabbing the user's IP address
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)
hth,
C.
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();
C.
Chandesh Parekh
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
Joomla & WordPress Web Development
twitter : http://www.twitter.com/chandeshparekh
web : http://chandeshparekh.com
- RustyJoomla
- Joomla! Explorer
- Posts: 409
- Joined: Wed Aug 01, 2007 12:08 am
- Location: UK
- Contact:
Re: Grabbing the user's IP address
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
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