J3 Issue ?
avatar innato
innato
30 Aug 2018

I wonder if the Joomla! PHP Session Handler is really working as intended. I found rather surprising results.

I was not able to find whether this issue has already been raised. If so, I appologise.
Tested with latest Joomla! 3.8.12, standard install, with PHP 7.2 and MySQL 5.7.

I set the global config System / Session Settings / Session Handler to 'PHP'

Steps to reproduce the issue

  • Joomla! 3.8.12 standard install.
  • Log into back-end (/administrator)
  • Set the global config System / Session Settings / Session Handler to 'PHP'
  • Create a username Reg belonging to User Group 'Registered'.
  • Save config and go to System/Control Panel => the back-end dashboard.

ISSUE 1:

  • Open browser 1 (e.g. Firefox) and log into the front-end with username Reg.
  • Open browser 2 (e.g. Opera) and log into the front-end with username Reg.
  • You have now two sessions active: session 1 and session 2.
  • At the back-end dashboard, you will see that user Reg has logged in twice (under caption "Logged-In Users").
  • The Joomla! table #_session will contain both these sessions.
  • Now log out session 1 via the front-end (Firefox): BOTH session by Reg disappear from the back-end dashboard AND the table #_session. BUT session 2 (Opera) is still active at the front-end (i.e. has not been logged out)!
  • A bit later, say 30-45 seconds, and after some clicking back and forth (front-end and back-end), session 2 reappears in both the table #_session AND the back-end dashboard. WEIRD!

ISSUE 2:

  • It is not possible to force a logout from the back-end dashboard, i.e. by clicking the red icon with the white cross.
  • Log into front-end as user Reg. Then click red icon w/ white cross next to this user at the back-end dashboard. The icon and user disappears. At the front-end however, the user is not logged out and when refreshing the front-end page, the red icon reappears in the back-end dashboard.

ISSUE 3:

  • With session handler set to PHP, the Joomla! table #_session remains to be used. The table is regularly (but not instantly!) synchronised with the PHP session data, but this synchronisation does not include the updating of the timestamp field 'time' in table #_session. With session handler set to 'database', the time field is updated every time the web page is refreshed or another page is visited. This lack of updating introduces a discrepancy between PHP data and table #_session.
    It would be great for 3rd party plug-ins to have the table #_session 'time' field updated to session.time.now for each refreshed/new page of the related session.

Expected result

  • Issue 1: Consistency, at all times, between what back-end dashboard is displaying and what actually happens at the front-end (ref logged-in or not). Instant synchronisation between PHP data and table #_session.
  • Issue 2: Resolve.
  • Issue 3: Field 'time' of table #_session always contains the timestamp of the most recent session activity, as has been the case with Session Handler set to 'database'

Actual result

See under '# Steps to reproduce'

System information (as much as possible)

Joomla! 3.8.12, standard install, PHP 7.2.4, MySQL 5.7.21
Joomla! session handler (back-end config) set to PHP.

Additional comments

A timely and accurate synchronisation between PHP data files and Joomla! #_session table will resolve/prevent significant problems for 3rd party extension developers like myself. So please get it right!

avatar innato innato - open - 30 Aug 2018
avatar joomla-cms-bot joomla-cms-bot - labeled - 30 Aug 2018
avatar innato innato - change - 30 Aug 2018
The description was changed
avatar innato innato - edited - 30 Aug 2018
avatar innato innato - change - 30 Aug 2018
The description was changed
avatar innato innato - edited - 30 Aug 2018
avatar joeforjoomla
joeforjoomla - comment - 30 Aug 2018

@innato I confirm you the issue 3.
I've already faced this problem in one extension of mine that heavily rely on the #__session metadata 'time' field.

I had to manage a fix in my extension using the following code:

// Patch for propagating DB session lifetime if the native PHP sessions handler is active
$config = JFactory::getConfig ();
if($config->get ( 'session_handler' ) != 'database') {
try {
$time = time();
// The modulus introduces a little entropy, making the flushing less accurate but fires the query less than half the time.
$query = $this->_db->getQuery(true)
->update($this->_db->quoteName('#__session'))
->set($this->_db->quoteName('time') . ' = ' . $time)
->where($this->_db->quoteName('session_id') . ' = ' . $this->_db->quote($this->userSessionTable->session_id));

			$this->_db->setQuery($query);
			$this->_db->execute();
		} catch (Exception $e) {
			// No errors managed, leave the process go on anyway
		}
	}
avatar mbabker
mbabker - comment - 30 Aug 2018

The time column isn't really considered part of the metadata aspect of that table and that's why we don't touch it (if you are using the database handler that does manage the time column; as there's no hook point when session data is written any updates to the time column would be arbitrary at best and still leave a lot of room for discrepancy if you're relying on that for something like determining expiry; this all isn't to say the behavior can't change but if someone's going to consider a patch the change has to be aware of the fact that the time column value MUST respect the value set by the session handler and not an outside thing like the metadata manager when the database handler is used as a backend otherwise you run the risk of a security issue related to sessions not expiring when intended, one more reason why mixing the two types of data into one table was IMO a not-so-great architectural decision).

avatar mbabker
mbabker - comment - 30 Aug 2018

Regarding item 1, there might be a semi-aggressive database query somewhere in the logout process, but when the database session handler is not in use the data in the session table is nothing more than a convenience for core features such as a list of logged in users or the frontend who's online module (or whatever extension integrations are made), the point of truth as it relates to sessions will be whatever engine is in use. So the fact these things "disappear and reappear" is at most annoying, at worst an inconvenience.

Also keep in mind the metadata storage process has been completely refactored in 4.0, to the point that if the data fails to write for whatever reason it is no longer considered a fatal error (in 3.x it is because the database session handler doesn't know how to perform an INSERT query), and that the configuration will allow processing of this metadata to be disabled. Now granted, the odds of the INSERT/UPDATE query for metadata failing and the rest of the request cycle completing successfully are going to be slim to none, but this does make it more theoretically possible that the metadata will be out of sync with the true session data source (unless your source is the database), where at worst you'd be missing the user and client IDs).

avatar brianteeman brianteeman - change - 30 Aug 2018
Labels Added: J3 Issue
avatar brianteeman brianteeman - labeled - 30 Aug 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 31 Aug 2018
Status New Discussion
avatar innato
innato - comment - 31 Aug 2018

@mbabker Thanks for your valuable comments. You regard item 1 as annoying/inconvenient. I would rather call it sloppy. Same for item 2; if admin can't force a logout, then don't suggest it by displaying a red/white icon. As to the #_session table, an extension of mine depends on this table and indeed it needs up-to-date data for expiry reasons (also related item 3, the time field).
In your last comment, are you saying that things will be very different in J4.0 ? Would you say that the current release Alpha 3 is close to what the metadata storage will be? If so, I can start testing it. Or maybe I shall add an extra system plug-in to my extension to store the needed session data independently from the Joomla! core...

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 31 Aug 2018

current release Alpha 3

current Release is https://github.com/joomla/joomla-cms/releases/tag/4.0.0-alpha4

avatar brianteeman
brianteeman - comment - 31 Aug 2018

Because j4 is developing rapidly its far better to grab the nightly release https://developer.joomla.org/nightly-builds.html

avatar brianteeman
brianteeman - comment - 31 Aug 2018

@innato As a developer you are welcome to submit a pull request to address the "sloppy" code

avatar innato
innato - comment - 31 Aug 2018

@brianteeman Just to make things clear... I didn't mean that the code is sloppy, but the outcome of it is. I hope you can give me some credit for not being a native English speaker. I have been doing Joomla! for ages (starting J1.0), but never got involved in pull requests, don't even know exactly what they are... I'll start reading about it. Rgrds.

avatar mbabker
mbabker - comment - 31 Aug 2018

You regard item 1 as annoying/inconvenient. I would rather call it sloppy.

Sloppy, inconvenient, at the end of the day it all basically means the same thing, something isn't behaving consistently.

As to the #_session table, an extension of mine depends on this table and indeed it needs up-to-date data for expiry reasons (also related item 3, the time field).

Unfortunately, unless you're using the database handler this isn't reliable. We generally write the metadata insert/update at the start of the request cycle, session data can be written at any time after. For quick requests that start/end in the same second this can be OK, but if you've got a long running request that rolls across seconds then the expiry timestamp in the session data and the timestamp in the session table (at that point only acting as a metadata store) would be out-of-sync. If you're relying on these to be 100% in sync that just won't happen. So even if we improve the handling here, it's not a safe value to rely on in all scenarios.

In your last comment, are you saying that things will be very different in J4.0 ?

The process on how the metadata is managed will be a bit different, as well as the ability to turn it off completely being present, but those internal changes shouldn't affect extensions using this data by the time all is said and done (there's an open issue somewhere detailing some remaining tasks to complete related to the session handling in general).

avatar innato
innato - comment - 28 Oct 2018

As to the #_session table, an extension of mine depends on this table and indeed it needs up-to-date data for expiry reasons (also related item 3, the time field).

To resoIve the issue in the least time-consuming way, I have added a simple system plugin to my extension, which updates the timestamp in the #__session table. The timestamp is not critical on a seconds time scale, so the out-of-sync issue that @mbabker mentioned is not a problem.

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 2 Apr 2019

@innato is this an Issue anymore?

avatar franz-wohlkoenig franz-wohlkoenig - change - 2 Apr 2019
Status Discussion Information Required
avatar innato
innato - comment - 2 Apr 2019

@franz-wohlkoenig I haven't checked the present Joomla! version 3.9.4, but if the issue has not been addressed in a Joomla! version after J3.9.1, then it will still exist. But as I commented on 28th Oct 2018, I have worked around it.
In case you're checking because the issue is still open, I will be happy to close it, as Joomla! 4 is on its way soon...

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 2 Apr 2019

In case you're checking because the issue is still open, I will be happy to close it, as Joomla! 4 is on its way soon...

Yes, that was the Reason.

avatar innato
innato - comment - 2 Apr 2019

Thank you for waking me up. Issue closed.

avatar innato innato - change - 2 Apr 2019
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2019-04-02 15:38:59
Closed_By innato
avatar innato innato - close - 2 Apr 2019

Add a Comment

Login with GitHub to post a comment