Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing session data outside Joomla

Tags:

php

joomla

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 about doing that? Is there a file which I can include? I tried using $_SESSION but it shows empty.

Is there a simple solution to my problem? Thank you for your time.

like image 939
Alec Smart Avatar asked Jul 24 '09 03:07

Alec Smart


People also ask

Can user access session data?

No they can not. Session information is stored server-side not client-side.

How do I access session variables?

You can access a session variable's value by using the global variable $_SESSION. In the example stated below, you will create another session with a variable that stores your name. session_start();

How can I see PHP session in browser?

Session data is server-side, while cookies are client-side. The session cookie contains the session identifier, which the server (i.e.: PHP) uses to retrieve the proper session data. It is not possible to view session data without remote access to the server, or using a script (that resides on the server).

Where can a session data be stored?

How many ways can a session data be stored? Explanation: Within flat files(files), within volatile memory(mm), using the SQLite database(sqlite), or through user defined functions(user).


1 Answers

Actually that's not as easy as it sounds. Joomla uses its own session handling with come unique session-id-generation and some encryption in place, so the only way to get into the Joomla session data is to use the appropriate Joomla functions (as others have suggested). I recently had a project where we needed to transfer a Joomla authenticated user into a separate application. We did this by adding a Joomla adapter which instantiates the Joomla user classes, reads the user data, puts everything into an encrypted cookie and redirects back to our application. In there we read the encrypted cookie, instantiate our own user object and discard the cookie. As this is not 100% secure we're changing the system to write the user data in a database table and read it from our application - we avoid the unsecure way through a cookie that way, because even though the cookie is encrypted (and contains sensitive user information which suffice to authenticate a user) it'll be transfered on wire and could be sniffed.

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );

require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');

$mainframe = JFactory::getApplication('site');

The above is the basic script required to access Joomla resources.

like image 144
Stefan Gehrig Avatar answered Sep 29 '22 18:09

Stefan Gehrig