Index: mollom.module
===================================================================
--- mollom.module	(revision 8265)
+++ mollom.module	(working copy)
@@ -1161,9 +1161,8 @@
  * The 'mollom' form element is stateful. The Mollom session ID that is exchanged
  * between Drupal, the Mollom back-end, and the user allows us to keep track of
  * the form validation state.
- * The session ID is valid for a specific user session and a given $form_id
- * only. We expire it as soon as the form is submitted, to avoid it being
- * replayed.
+ * The session ID is valid for a given $form_id only. We expire it as soon as
+ * the form is submitted, to avoid it being replayed.
  */
 function mollom_process_mollom($element, $input, &$form_state, $complete_form) {
   // Setup initial Mollom session and form information.
@@ -1222,17 +1221,16 @@
  */
 function mollom_process_mollom_session_id($element, $input, &$form_state) {
   // The current state can come either from the $form_state, if the form
-  // was just rebuilt in the same request or from data posted by the user. In
-  // that case we validate that the correct form_id and user session ID is used.
+  // was just rebuilt in the same request, or from data posted by the user. In
+  // the latter case the state is fetched from the temporary data store. It is
+  // verified that the session was created for the current form and that it has
+  // not expired or already been used.
   if (empty($form_state['mollom']) && !empty($input['session_id'])) {
     @list($timestamp, $mollom_session_id) = explode('-', $input['session_id'], 2);
 
     if (empty($mollom_session_id)) {
       watchdog('mollom', 'Bogus session id %session.', array('%session' => $form_state['input']['mollom']['session_id']), WATCHDOG_WARNING);
     }
-    elseif (!isset($_SESSION['mollom_sessions'][$mollom_session_id])) {
-      watchdog('mollom', 'Non-existent session id %session.', array('%session' => $mollom_session_id));
-    }
     elseif (!$cache = cache_get($mollom_session_id, 'cache_mollom')) {
       if (time() - $timestamp > 30 * 60) {
         watchdog('mollom', 'Expired session id %session.', array('%session' => $mollom_session_id));
@@ -1279,6 +1277,11 @@
     return _mollom_fallback();
   }
 
+  // If a new session ID was generated by Mollom, flush the old session from the
+  // database cache.
+  if (!empty($form_state['mollom']['session_id']) && $result['session_id'] != $form_state['mollom']['session_id']) {
+    cache_clear_all($form_state['mollom']['session_id'], 'cache_mollom');
+  }
   // Assign the session ID returned by Mollom.
   $form_state['mollom']['session_id'] = $result['session_id'];
   // Store the response for #submit handlers.
@@ -1399,6 +1402,11 @@
       $captcha .= '</a> (<a href="#" class="mollom-audio-captcha">' . t('play audio CAPTCHA') . '</a>)';
       $element['captcha']['#field_prefix'] = $captcha;
 
+      // If a new session ID was generated by Mollom, flush the old session from
+      // the database cache.
+      if (!empty($form_state['mollom']['session_id']) && $result['session_id'] != $form_state['mollom']['session_id']) {
+        cache_clear_all($form_state['mollom']['session_id'], 'cache_mollom');
+      }
       // Assign the session ID returned by Mollom.
       $form_state['mollom']['session_id'] = $result['session_id'];
       $form_state['mollom']['response']['session_id'] = $result['session_id'];
@@ -1422,15 +1430,6 @@
   $timestamp = time();
   if (!empty($form_state['mollom']['session_id'])) {
     $element['session_id']['#value'] = $timestamp . '-' . $form_state['mollom']['session_id'];
-
-    // Store the Mollom session id in the user session to force a
-    // session for anonymous users in Drupal 7 and Drupal 6 Pressflow.
-    // @see mollom_exit()
-    // @see mollom_form_submit()
-    $_SESSION['mollom_sessions'][$form_state['mollom']['session_id']] = $timestamp;
-  }
-
-  if (!empty($form_state['mollom']['session_id'])) {
     cache_set($form_state['mollom']['session_id'], $form_state['mollom'], 'cache_mollom', $timestamp + 21600);
   }
 
@@ -1459,11 +1458,9 @@
       mollom_data_save($form_state['mollom']['entity'], $data['post_id']);
     }
   }
-  // Flush Mollom session information from database cache and user session.
+  // Flush Mollom session information from database cache.
   if (!empty($form_state['mollom']['session_id'])) {
-    $session_id = $form_state['mollom']['session_id'];
-    cache_clear_all($session_id, 'cache_mollom');
-    unset($_SESSION['mollom_sessions'][$session_id]);
+    cache_clear_all($form_state['mollom']['session_id'], 'cache_mollom');
   }
   // Remove Mollom session information from form state to account for unforeseen
   // new builds of the form.
@@ -1471,25 +1468,6 @@
 }
 
 /**
- * Implements hook_exit().
- */
-function mollom_exit() {
-  // Expire all mollom session IDs as soon as possible.
-  $now = time();
-  if (isset($_SESSION['mollom_sessions'])) {
-    foreach ($_SESSION['mollom_sessions'] as $id => $timestamp) {
-      if ($now - $timestamp > 30 * 60) {
-        unset($_SESSION['mollom_sessions'][$id]);
-      }
-    }
-  }
-  // If all sessions were removed, also remove our storage key.
-  if (isset($_SESSION) && empty($_SESSION['mollom_sessions'])) {
-    unset($_SESSION['mollom_sessions']);
-  }
-}
-
-/**
  * @} End of "defgroup mollom_form_api".
  */
 
