Skip to content


Custom ("user") session handler using database (ie MySQL) instead of "files" #1279

robocoder opened this Issue · 25 comments

3 participants


Use Zend_Session_SaveHandler_DbTable.


For Installation, we would store session information in a signed cookie, and delete the cookie when the installation is complete.


  • reduce number of inodes, eliminating tmp/sessions
  • move the session handling logic out of ./index.php (to FrontController init?)
  • potentially simplifies the session handling logic
  • no longer second guessing security of user's configuration
  • works with load balanced web servers that don't share session files
  • works when user doesn't have write access to shared session folder (technically, considered to be an improperly configured web server)


  • cookies subject to 4K limit (only applies to installer and custom dashboard layout for anonymous)
  • slower (especially with garbage collection)
  • would have to rework LanguagesManager/API.php's use of Zend_Session_Namespace in getLanguageForSession() and setLanguageForSession()

Note: Piwik still requires write access to ./tmp (i.e., templates_c, cache/tracker, latest).

While you're at it, add a systemCheck for the 'session' extension, in case php was compiled with --disable-session.

And look into handling disabled ini_set() -- used by Zend_Session.

Oh, and make sure we handle the case where session.use_cookies = 0 (ref:


(In [2121]) refs #1279 - refactor session initialization code out of index.php


(In [2122]) refs #1279 - fallback to system default


(In [2280]) refs #1279 - test that session.save_path is also readable, otherwise
session file cleanup may fail, e.g.,

Zend_Session_Exception: Zend_Session::start() - /PATH/libs/Zend/Session.php(Line:480): Error #8 session_start(): ps_files_cleanup_dir: opendir(/var/lib/php5) failed: Permission denied


I've confirmed on my fresh install that this patch fixes the exception output. Thank you.


Reprioritizing. On the demo server, I noticed we have 285000+ session files in piwik/tmp/sessions -- most being empty files. That's a lot of inodes, and could be a problem for some users with shared hosting accounts.


Nice to have:

  • eliminate dependency on libs/Zend/Session and the session extension. (No more workarounds.)

(In [3514]) refs #1279, refs #1910 - language preference for anonymous users is now stored in a cookie; we can't use this workaround for the dashboard layout because of cookie size limits


(In [3515]) refs #1279, refs #1910 - fix typo


Other suggestions:

  • not every request requires session data; propose not calling Zend_Session::start() in Piwik_Session; ie lazy init
  • Dashboard: set expiry for the anonymous user's non-persistent dashboard layout, eg $ns->setExpirationSeconds(1800);

Using a DB table should avoid the potential race condition experienced with locked session files (see #2296).


(In [4761]) refs #1279 - add session table


(In [4762]) refs #1279 - add update script and bump version


(In [4763]) refs #1279 - fix typo


(In [4764]) refs #1279 - typo


(In [4765]) fixes #1279


CI shows the update fails:

[22-May-2011 19:05:51] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'piwik_qa.canoo_session' doesn't exist' in /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('DESCRIBE `canoo...', Array)
#4 /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/core/Db/Adapter/Pdo/Mysql.php(211): Zend_Db_Adapter_Pdo_Abstract->query('DESCRIBE `canoo...', Array)
#5 /home/www/dat in /home/www/data/root/jenkins.private/jobs/Piwik/workspace/build/libs/Zend/Db/Statement/Pdo.php on line 234
Piwik Open Source Analytics member


  • Why move Piwik_Session::start to frontController instead of previous location in index.php?
  • Session purge: what is the default timeout after which sessions will be deleted from the DB to ensure the table doesn't grow out of control?

This is great feature because it will ensure that load balanced Piwik Reporting UI will work fine without requiring to "stick" same users to the same backend server!

Piwik Open Source Analytics member

(In [4766]) Refs #1279 Important to copy paste the table definitions in update files: this ensures that subsequent "ALTER" on these tables will work fine (otherwise, if we change table definition in myisam file in 1.8, and users upgrade from 1.4 to 1.9, the 1.5 upgrade will install the newest version of 1.8 tables which will then fail the 1.8 ALTER table on this table..)


I had to move the Piwik_Session::start() to front controller because the save handler needs the $db handle.

Purge: the frequency of cleanup and lifetime is defined in php.ini (e.g., session.gc_maxlifetime)

re: r4767 Good point. I'll go back and fix those cases in 0.2.10, 0.2.13, and 0.2.27, so we don't set a bad example. ;)

I'm just installing 1.0 now, and going through the auto-update to find out what's causing the problem in comment:30.


(In [4768]) refs #1279

Piwik Open Source Analytics member

on the update screen I see an error at the bottom, not important but if easy to hide would be nice :) (see attached file)

Piwik Open Source Analytics member

Piwik Update.png


(In [4772]) refs #1279


Yes, I saw the exception too. This is fixed in r4772. I'm just waiting for confirmation from Jenkins before I close this ticket.


Thank you, Jenkins.

@robocoder robocoder added this to the 1.5 - Piwik 1.5 milestone
@robocoder robocoder self-assigned this
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.