diff --git a/config-templates/config.php b/config-templates/config.php
index be9e75a17db4f0230a9f5cf3bff03df837b16d0d..4e001c7b6423c2205e2bc8097a870c1e18bcdf6b 100644
--- a/config-templates/config.php
+++ b/config-templates/config.php
@@ -106,6 +106,9 @@ $config = array (
 	'session.duration'		=>  8 * (60*60), // 8 hours.
 	'session.requestcache'	=>  4 * (60*60), // 4 hours
 	
+	'session.phpsession.cookiename'  => null,
+	'session.phpsession.limitedpath' => false,
+	
 	/*
 	 * Languages available and what language is default
 	 */
diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index f449d4294fd8a20948eb2a69ddc4cc564cefe3de..ac27693ded79a516f51046112c1f93ea9ef544d9 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -117,6 +117,9 @@ class SimpleSAML_Session implements SimpleSAML_ModifiedInfo {
 		}
 	}
 	
+	
+	
+	
 	public static function init($authenticated = false, $authority = null) {
 		
 		$preinstance = self::getInstance();
diff --git a/lib/SimpleSAML/SessionHandlerPHP.php b/lib/SimpleSAML/SessionHandlerPHP.php
index 1466e0e2ec2dd2290846736bf6b07e2f41150619..3f4df18ad2f5fad32aded07c500125c907189c52 100644
--- a/lib/SimpleSAML/SessionHandlerPHP.php
+++ b/lib/SimpleSAML/SessionHandlerPHP.php
@@ -31,6 +31,14 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler {
 		 * started, and we should avoid calling session_start().
 		 */
 		if(session_id() === '') {
+			$config = SimpleSAML_Configuration::getInstance();
+			
+			$cookiepath = ($config->getValue('session.phpsession.limitedpath', FALSE) ? '/' . $config->getValue('baseurlpath') : '/');
+			session_set_cookie_params(0, $cookiepath, NULL, SimpleSAML_Utilities::isHTTPS());
+			
+			$cookiename = $config->getValue('session.phpsession.cookiename', NULL);
+			if (!empty($cookiename)) session_name($cookiename);
+			
 			session_start();
 		}
 	}
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 558779468df655a34a356003cff8e4c382a683e2..254ea9d73b276c56415356ed266af854ad36e948 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -60,6 +60,27 @@ class SimpleSAML_Utilities {
 	
 	}
 	
+	/**
+	 * This function checks if we should set a secure cookie.
+	 *
+	 * @return TRUE if the cookie should be secure, FALSE otherwise.
+	 */
+	public static function isHTTPS() {
+
+		if(!array_key_exists('HTTPS', $_SERVER)) {
+			/* Not a https-request. */
+			return FALSE;
+		}
+
+		if($_SERVER['HTTPS'] === 'off') {
+			/* IIS with HTTPS off. */
+			return FALSE;
+		}
+
+		/* Otherwise, HTTPS will be a non-empty string. */
+		return $_SERVER['HTTPS'] !== '';
+	}
+	
 	/**
 	 * Will return https://sp.example.org/universities/ruc/baz/simplesaml/saml2/SSOService.php
 	 */