Development

#1643 (sfMySQLSessionStorage garbage collection fails due to session_start call before connection is initialized)

You must first sign up to be able to contribute.

Ticket #1643 (new defect)

Opened 2 years ago

sfMySQLSessionStorage garbage collection fails due to session_start call before connection is initialized

Reported by: smeves Assigned to: fabien
Priority: major Milestone:
Component: other Version: 1.0.0
Keywords: sfMySQLSessionStorage session mysql Cc:
Qualification:

Description

When using sfMySQLSessionStorage class for session handling, the initialize method is called which defines the sessions handlers and then starts the session using session_start(). As mentioned in the PHP manual, garbage collection occurs during session start. When this garbage collection method is invoked an sfDatabaseException is thrown because no connection parameters have been set for the database.

Fatal error: Uncaught exception 'sfDatabaseException'
with message 'MySQLSessionStorage cannot delete old sessions' in
/symfony/lib/storage/sfMySQLSessionStorage.class.php:153 
Stack trace: 
#0 /symfony/lib/storage/sfMySQLSessionStorage.class.php(76): sfMySQLSessionStorage->sessionGC(1440) 
#1 /site/cache/frontend/stage/config/config_factories.yml.php(17): sfMySQLSessionStorage->initialize()
#2 /symfony/lib/util/sfContext.class.php(70): require('...')
#3 /symfony/lib/util/sfContext.class.php(87): sfContext->initialize(Object(sfContext), Array)
#4 /site/web/index.php(10): sfContext::getInstance() 
#5 {main} thrown in /symfony/lib/storage/sfMySQLSessionStorage.class.php on line 153

The current garbage collection method fails because no database connection resource as been defined:

  public function sessionGC($lifetime)
  {
    // determine deletable session time
    $time = time() - $lifetime;

    // get table/column
    $db_table    = $this->getParameterHolder()->get('db_table');
    $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time');

    // delete the record associated with this id
    $sql = 'DELETE FROM '.$db_table.' '.
           'WHERE '.$db_time_col.' < '.$time;

    if (@mysql_query($sql, $this->resource))
    {
      return true;
    }

    // failed to cleanup old sessions
    $error = 'MySQLSessionStorage cannot delete old sessions';

    throw new sfDatabaseException($error);
  }

The proper fix would be to make sure the sfMySQLSessionStorage->resource value has been set in the sessionGC() function before attempting to clean up the old sessions.