I am trying to run an application outside Joomla (not as a plugin) and I would like to access the logged in user\'s information (userid). I am wondering how should I go abou
The solution is to set the session for your whole domain and/or site. It applies if you're trying to access the session data outside of joomla scope. For example, if your joomla site is located on http://example.com/joomla/ and your other site on http://othersite.example.com/ then the cookie holding the session id is not transmitted from joomla to the other site. To modify this behaviour, use session_ set_ cookie_ params before every session_start() (I don't know joomla very well, but you should have to add only a few lines of code). Use it this way:
session_set_cookie_params(86400, '/', '.example.com');
86400 is the lifetime of the session, set it to what you prefer (86400 is one day). '/' is the path of the cookie. It means that if your joomla site is located on http://example.com/joomla/ , the session cookie will still be sent if the user accesses http://example.com/ .
'.example.com' is the domain. Note the dot at the beginning, it's very important. It says that the session cookie will be sent on any subdomain of example.com. If you don't put it, the cookie will be sent only for addresses starting with http://example.com/ .
This should solve your problem, unless you are trying to access the session data from another domain. If it's the case, leave a comment here, I'll see if I cand find something.
A solution for Joomla 3, without using any libraries.
require_once '../configuration.php'; // load Joomla configuration file
$jConfig = new \JConfig();
$secret = $jConfig->secret;
$dbprefix = $jConfig->dbprefix;
$cookieName = md5(md5($secret . 'site'));
$sessionId = $_COOKIE[$cookieName];
$sql = "select userid from {$dbprefix}session where client_id = 0 and session_id = ?";
$userId = $db->lookup($sql, [$sessionId]);
(The code above is simplified, without any error handling.)
It is very possible that like Wordpress, Joomla doesn't use any 'session' data, but rather pulls data directly from the database. In which case you would need to use Joomla's native functions.
But that is just from my experience with Wordpress.
Updated
I guess I was wrong.
Supposidly this is the api class for accessing Joomla Session variables:
// Returns a reference to the global JSession object, only creating it if it doesn't already exist $session = &JFactory::getSession();
// Get a value from a session var $value = $session->get('var_name', null);
// Put a value in a session var $session->set('var_name', $value);
If you store your sessions in database, you could decode session data as in this comment:
http://www.php.net/manual/en/function.session-decode.php#79244
I cannot tell you how Joomla with versions above 1.5 does that but in Joomla 1.5 here is how you do that: ( I am sure for other versions procedure is very similar )
Joomla generates Unique session id for front-end of the website and back-end. To access session data all you need is know the session id.
In joomla configuration file there is a parameter called "secret"
For back-end this is how you generate session id:
$session_id = md5( md5( JConfig::$secret.'administrator' ) );
and for front end:
$session_id = md5( md5( JConfig::$secret.'site' ) );
After this a simple query
mysql_query( 'SELECT `data` FROM jos_session WHERE session_id="'.$sessionId.'" )
will give you access to session data. All you need is to decrypt it with session_decode and session data will be in $_SESSION variable.
Don't forget to put session_start before session_decode otherwise it will not work
To get Joomla user id use:
$user =& JFactory::getUser();
$user_id = $user->get('id');
and to get user session id use:
$session = & JFactory::getSession();
$session_id = $session->getId();