Index: securepages.module
===================================================================
--- securepages.module	(revision 13713)
+++ securepages.module	(working copy)
@@ -175,6 +175,7 @@
   global $base_root;
   
   $_SESSION['securepages_redirect'] = TRUE;
+  securepages_redirected_this_request(TRUE);
   $path = !empty($_REQUEST['q']) ? $_REQUEST['q'] : '';
   $query = count($_GET) > 1 ? securepages_get_query($_GET) : NULL;
   $url = securepages_url($path, array('query' => $query, 'secure' => $secure));
@@ -473,4 +474,26 @@
   }
   
   return TRUE;
-}
\ No newline at end of file
+}
+
+/**
+ * Record whether we are doing a redirect on this request
+ */
+function securepages_redirected_this_request($set = FALSE) {
+  static $redirected = FALSE;
+  $return = $redirected;
+  $redirected = $set;
+  return $return;
+}
+
+/**
+ * Implementation of hook_exit(). Makes sure the session variable is
+ * cleared even if serving the page from cache as hook_exit is still
+ * called except in aggressive caching mode with which securepages is
+ * not compatible 
+ */
+function securepages_exit() {
+  if (!securepages_redirected_this_request()) {
+    unset($_SESSION['securepages_redirect']);
+  }
+}
