Calling the Piwik API and get an autostart.session error

(bronco) #1

Hey guys,
I try to build an drupal modul wich allows to automatically adds an User and his websites to the piwik app. I build already the drupal modul and the piwik connector. After the registration at drupal the function add_piwik_user is called and I get this error .

Fatal error: Uncaught exception ‘Zend_Session_Exception’ with message ‘session has already been started by or session_start()’

I tried 3 things:

  1. Add htaccess with the php flag php_flag session.auto_start off (does not work)
  2. php.ini check if autostart.session is 0 (is 0)
  3. Try to uncomment the Zend_Session::start(true); in namespace.php
  4. Put this to my @ini_set(‘session.auto_start’, 0);

All of these solutions doesn’t work.

My Server config:
Apache 2.2
Php 2.9
Centos (maybe useful…?)

I call the api in php way. Like this:
define(‘PIWIK_ENABLE_DISPATCH’, false);
@ini_set(‘session.auto_start’, 0);

include_once “sites/all/modules/contrib/piwik_tracker/piwik/index.php”;
include_once “sites/all/modules/contrib/piwik_tracker/piwik/core/API/Request.php”;

What I’ doing wrong for calling the Piwik Api ?

(bronco) #2

I forgot to tell that I use additionally memcache for sessions. Maybe this info is useful.


I have the exact same problem, would be really grateful for a solution to this… I’m calling Piwik like this:

  // if you don't include 'index.php', you must also define PIWIK_DOCUMENT_ROOT
  define('PIWIK_INCLUDE_PATH', $path);
  define('PIWIK_USER_PATH', $path);
  define('PIWIK_ENABLE_DISPATCH', false);
  define('PIWIK_ENABLE_ERROR_HANDLER', false);
  define('PIWIK_ENABLE_SESSION_START', false);
  require_once PIWIK_INCLUDE_PATH . "/index.php";
  require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";

  // This inits the API Request with the specified parameters
  $request = new Piwik_API_Request($query);
  // Calls the API and return result
  return $request->process();

My “session.auto_start” is off, so as far as I can tell I am either not closing a session when I should be, or piwik API isn’t checking whether or not a session is open before making a call…


I should note that this occurs after rapid, repeated calls to the API (adding many domains).

(vipsoft) #5

Is there any difference if you initialize the FrontController only once?


How would I do that? I have created a function that calls the Piwik API, so my script basically calls the function for each operation. Can I do I check to see if the frontController has already been initialized?


My understanding of the Piwik code is basically nonexistent, so suspect this is not the best fix for the above suggestion by Anthon, but I seem to have resolved the problem by replacing this:

//some function that calls the piwik api
function call_piwik_api($input) {

with this:

//some function that calls the piwik api
function call_piwik_api($input) {
  global $piwik_frontcontroller_called;
  if ( ! $piwik_frontcontroller_called ) {
    $piwik_frontcontroller_called = TRUE;

Thanks Anthon, and if you could post a nicer check for if the FrontController has been called that’d be great!

(vipsoft) #8

That’s the right idea. The only thing I’d do different is use the “static” keyword instead of “global”.


Ok cheers. Just a thought though: doesn’t this represent somewhat of a flaw in the API? I mean, APIs are by definition used programmatically, so rapidly repeated calls should have been expected in the design; why isn’t this check in the API itself?

(vipsoft) #10

Well, yours is a special use case. The internal classes and methods used by Piwik are subject to change, although we try not to (in order to avoid breaking plugins), but may be necessary for bug fixes, enhancements, or code improvement. What we do try to keep stable is the REST API.