How to get user ip from user session?

431 views
Skip to first unread message

Viper

unread,
Aug 22, 2013, 9:25:59 AM8/22/13
to joomla-de...@googlegroups.com
I'm trying the next code:
$config = JFactory::getConfig();
$ip = JSession::getInstance($config->get('session_handler'), array('name'=>'__default'))->get('session.client.address');


But variable is empty. Any ideas how to get ip?

Fedor Vlasenko

unread,
Sep 17, 2013, 9:44:13 AM9/17/13
to joomla-de...@googlegroups.com
function getIP() {
    if (getenv("HTTP_CLIENT_IP")) return getenv("HTTP_CLIENT_IP");
    if (getenv("HTTP_X_FORWARDED_FOR")) return getenv("HTTP_X_FORWARDED_FOR");
    if (getenv("REMOTE_ADDR")) return getenv("REMOTE_ADDR");
    return "UNKNOWN";
}

Nicolay Ramm

unread,
Sep 17, 2013, 10:42:06 AM9/17/13
to joomla-de...@googlegroups.com
One comment:

Both HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR can be set to any value either by intermediate proxy servers or by the originating client itself. Only REMOTE_ADDR is set by the webserver itself, and is always the actual IP address where the request originated. This is in fact the only IP address you can know for sure in any case. I mention this because if you for example want to restrict access to a page based on IP address, REMOTE_ADDR is the only value you can rely on.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages