diff --git a/docs/simplesamlphp-advancedfeatures.txt b/docs/simplesamlphp-advancedfeatures.txt
index c1af38f4f614c798a10ffe427102ab31629aee45..05141351fe13c3563559ee62994621ec61e8deba 100644
--- a/docs/simplesamlphp-advancedfeatures.txt
+++ b/docs/simplesamlphp-advancedfeatures.txt
@@ -189,7 +189,7 @@ Example code for the function with GeoIP country check:
         }
 
         if ($init) {
-            $session->setData($data_type, $data_key, $remote_addr);
+            $session->setData($data_type, $data_key, $remote_addr, SimpleSAML_Session::DATA_TIMEOUT_SESSION_END);
             return;
         }
 
@@ -209,7 +209,7 @@ Example code for the function with GeoIP country check:
 
         if ($country_a === $country_b) {
             if ($stored_remote_addr !== $remote_addr) {
-                $session->setData($data_type, $data_key, $remote_addr);
+                $session->setData($data_type, $data_key, $remote_addr, SimpleSAML_Session::DATA_TIMEOUT_SESSION_END);
             }
 
             return TRUE;
diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index 4cfd11cb1af45cadba59680ee972507ab1e3677c..b7151a98b947f959e6fdabbace35b7df22beaabc 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -21,6 +21,13 @@ class SimpleSAML_Session {
 	const DATA_TIMEOUT_LOGOUT = 'logoutTimeout';
 
 
+	/**
+	 * This is a timeout value for setData, which indicates that the data
+	 * should never be deleted, i.e. lasts the whole session lifetime.
+	 */
+	const DATA_TIMEOUT_SESSION_END = 'sessionEndTimeout';
+
+
 	/**
 	 * The list of loaded session objects.
 	 *
@@ -804,6 +811,11 @@ class SimpleSAML_Session {
 					continue;
 				}
 
+				if ($info['expires'] === self::DATA_TIMEOUT_SESSION_END) {
+					/* This data never expires. */
+					continue;
+				}
+
 				if($ct > $info['expires']) {
 					unset($typedData[$id]);
 				}
@@ -874,7 +886,7 @@ class SimpleSAML_Session {
 	public function setData($type, $id, $data, $timeout = NULL) {
 		assert('is_string($type)');
 		assert('is_string($id)');
-		assert('is_int($timeout) || is_null($timeout) || $timeout === self::DATA_TIMEOUT_LOGOUT');
+		assert('is_int($timeout) || is_null($timeout) || $timeout === self::DATA_TIMEOUT_LOGOUT || $timeout === self::DATA_TIMEOUT_SESSION_END');
 
 		/* Clean out old data. */
 		$this->expireData();
@@ -902,6 +914,8 @@ class SimpleSAML_Session {
 
 		if ($timeout === self::DATA_TIMEOUT_LOGOUT) {
 			$expires = self::DATA_TIMEOUT_LOGOUT;
+		} elseif ($timeout === self::DATA_TIMEOUT_SESSION_END) {
+			$expires = self::DATA_TIMEOUT_SESSION_END;
 		} else {
 			$expires = time() + $timeout;
 		}