Index: mollom.module
===================================================================
--- mollom.module	(revision 1127)
+++ mollom.module	(working copy)
@@ -157,6 +157,23 @@
   cache_clear_all(NULL, 'cache_mollom');  
 }
 
+ /**
+  * Implementation of hook_init().
+  */
+function mollom_init() {
+  // Expire the mollom session IDs as soon as possible.
+  $timestamp = time();
+  foreach ($_SESSION['mollom_sessions'] as $session_id => $session_timestamp) {
+    if ($timestamp - $session_timestamp > 30 * 60) {
+      unset($_SESSION['mollom_sessions'][$session_id]);
+    }
+  }
+
+  if (empty($_SESSION['mollom_sessions'])) {
+    unset($_SESSION['mollom_sessions']);
+  }
+}
+
 /**
  * Helper function to load a Mollom session ID from the database.
  */
@@ -538,6 +555,9 @@
     if (!$mollom_session_id) {
       watchdog('mollom', "Mollom ID '%session' is badly formatted.", array('%session' => $edit['session_id']));
     }
+    else if (!isset($_SESSION['mollom_sessions'][$mollom_session_id])) {
+      watchdog('mollom', "Mollom ID '%session' not found in the current user session.", array('%session' => $mollom_session_id));
+    }
     else if (!$cache = cache_get($mollom_session_id, 'cache_mollom')) {
       if (time() - $timestamp > 30 * 60) {
         watchdog('mollom', "Mollom ID '%session' is invalid, probably expired.", array('%session' => $mollom_session_id));
@@ -549,9 +569,6 @@
     else if ($cache->data['#form_id'] !== $form_state['values']['form_id']) {
       watchdog('mollom', "Mollom ID '%session' has been tampered with: it was generated for form %form1, but is used on form %form2.", array('%session' => $mollom_session_id, '%form1' => $cache->data['#form_id'], '%form2' => $form_state['values']['form_id']));
     }
-    else if ($cache->data['#user_session_id'] !== session_id()) {
-      watchdog('mollom', "Mollom ID '%session' has been tampered with: it was generated for a user with sid %sid1, but is used by a user with sid %sid2.", array('%session' => $mollom_session_id, '%sid1' => $cache->data['#user_session_id'], '%sid2' => session_id()));
-    }
     else {
       $mollom_state = $cache->data;
     }
@@ -565,7 +582,6 @@
       '#require_analysis' => $element['#mode'] == MOLLOM_MODE_ANALYSIS,
       '#require_captcha' => $element['#mode'] == MOLLOM_MODE_CAPTCHA,
       '#passed_captcha' => FALSE,
-      '#user_session_id' => session_id(),
     );
   }
 
@@ -602,12 +618,17 @@
     // We save it in two places: as an hidden form field and in the cache
     // so that it persists form submission, and in $form_state so that it
     // persists form rebuilds.
+    $timestamp = time();
     $element['session_id'] = array(
       '#type' => 'hidden',
-      '#value' => time() .'-'. $mollom_state['#session_id'],
+      '#value' => $timestamp .'-'. $mollom_state['#session_id'],
     );
     cache_set($mollom_state['#session_id'], $mollom_state, 'cache_mollom', time() + 60*30);
 
+    // We store the mollom session into the user session in order to keep it
+    // open on Drupal 7 and Drupal 6 Pressflow.
+    $_SESSION['mollom_sessions'][$mollom_state['#session_id']] = $timestamp;
+    
     $form_state['mollom'] = $mollom_state;
   }
 
@@ -620,6 +641,7 @@
 function mollom_clean_state($form_id, $form_state) {
   if (!empty($form_state['values']['mollom']['session_id'])) {
     cache_clear_all($form_state['values']['mollom']['session_id'], 'cache_mollom');
+    unset($_SESSION[$form_state['values']['mollom']['session_id']]);
   }
 }
 
