37 private $previous_session =
array();
47 parent::__construct();
50 $this->cookie_name =
$config->getString(
'session.phpsession.cookiename', null);
52 if (function_exists(
'session_status') &&
defined(
'PHP_SESSION_ACTIVE')) {
53 $previous_session = session_status() === PHP_SESSION_ACTIVE;
55 $previous_session = (session_id() !==
'') && (session_name() !== $this->cookie_name);
58 if ($previous_session) {
59 if (session_name() === $this->cookie_name || $this->cookie_name === null) {
61 'There is already a PHP session with the same name as SimpleSAMLphp\'s session, or the '.
62 "'session.phpsession.cookiename' configuration option is not set. Make sure to set ".
63 "SimpleSAMLphp's cookie name with a value not used by any other applications." 71 $this->previous_session[
'cookie_params'] = session_get_cookie_params();
72 $this->previous_session[
'id'] = session_id();
73 $this->previous_session[
'name'] = session_name();
74 session_write_close();
77 if (!empty($this->cookie_name)) {
78 session_name($this->cookie_name);
80 $this->cookie_name = session_name();
83 $params = $this->getCookieParams();
85 session_set_cookie_params(
93 $savepath =
$config->getString(
'session.phpsession.savepath', null);
94 if (!empty($savepath)) {
95 session_save_path($savepath);
105 $cacheLimiter = session_cache_limiter();
106 if (headers_sent()) {
119 session_cache_limiter(
'');
121 session_cache_limiter($cacheLimiter);
138 if (empty($this->previous_session)) {
143 session_write_close();
145 session_name($this->previous_session[
'name']);
146 session_set_cookie_params(
147 $this->previous_session[
'cookie_params'][
'lifetime'],
148 $this->previous_session[
'cookie_params'][
'path'],
149 $this->previous_session[
'cookie_params'][
'domain'],
150 $this->previous_session[
'cookie_params'][
'secure'],
151 $this->previous_session[
'cookie_params'][
'httponly']
153 session_id($this->previous_session[
'id']);
154 $this->previous_session =
array();
155 $this->sessionStart();
162 parent::$sessionHandler = null;
174 $sessionId = bin2hex(openssl_random_pseudo_bytes(16));
190 if (!self::hasSessionCookie()) {
195 session_id(
$_COOKIE[$this->cookie_name]);
197 $session_cookie_params = session_get_cookie_params();
199 if ($session_cookie_params[
'secure'] && !HTTP::isHTTPS()) {
200 throw new \SimpleSAML_Error_Exception(
'Session start with secure cookie not allowed on http.');
203 $this->sessionStart();
215 return $this->cookie_name;
226 $_SESSION[
'SimpleSAMLphp_SESSION'] = serialize($session);
242 assert(
'is_string($sessionId) || is_null($sessionId)');
244 if ($sessionId !== null) {
245 if (session_id() ===
'') {
247 $ret = ini_set(
'session.use_cookies',
'0');
248 if (
$ret ===
false) {
249 throw new \SimpleSAML_Error_Exception(
'Disabling PHP option session.use_cookies failed.');
252 session_id($sessionId);
253 $this->sessionStart();
254 } elseif ($sessionId !== session_id()) {
255 throw new \SimpleSAML_Error_Exception(
'Cannot load PHP session with a specific ID.');
257 } elseif (session_id() ===
'') {
258 self::getCookieSessionId();
261 if (!isset(
$_SESSION[
'SimpleSAMLphp_SESSION'])) {
266 assert(
'is_string($session)');
283 return array_key_exists($this->cookie_name,
$_COOKIE);
302 $ret = parent::getCookieParams();
304 if (
$config->hasValue(
'session.phpsession.limitedpath') &&
$config->hasValue(
'session.cookie.path')) {
305 throw new \SimpleSAML_Error_Exception(
306 'You cannot set both the session.phpsession.limitedpath and session.cookie.path options.' 308 } elseif (
$config->hasValue(
'session.phpsession.limitedpath')) {
310 'session.phpsession.limitedpath',
312 ) ?
$config->getBasePath() :
'/';
315 $ret[
'httponly'] =
$config->getBoolean(
'session.phpsession.httponly',
true);
332 if ($cookieParams === null) {
333 $cookieParams = session_get_cookie_params();
336 if ($cookieParams[
'secure'] && !HTTP::isHTTPS()) {
338 'Setting secure cookie on plain HTTP is not allowed.',
339 CannotSetCookie::SECURE_COOKIE
343 if (headers_sent()) {
345 'Headers already sent.',
346 CannotSetCookie::HEADERS_SENT
350 session_set_cookie_params(
351 $cookieParams[
'lifetime'],
352 $cookieParams[
'path'],
353 $cookieParams[
'domain'],
354 $cookieParams[
'secure'],
355 $cookieParams[
'httponly']
358 if (session_id() !==
'') {
360 session_write_close();
364 $this->sessionStart();
setCookie($sessionName, $sessionID, array $cookieParams=null)
Set a session cookie.
static createSession($sessionId)
Create a new session and cache it.
hasSessionCookie()
Check whether the session cookie is set.
getCookieSessionId()
Retrieve the session ID saved in the session cookie, if there's one.
saveSession(\SimpleSAML_Session $session)
Save the current session to the PHP session array.
Attribute-related utility methods.
sessionStart()
This method starts a session, making sure no warnings are generated due to headers being already sent...
restorePrevious()
Restore a previously-existing session.
Create styles array
The data for the language used.
newSessionId()
Create a new session id.
getCookieParams()
Get the cookie parameters that should be used for session cookies.
loadSession($sessionId=null)
Load the session from the PHP session array.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
__construct()
Initialize the PHP session handling.
getSessionCookieName()
Retrieve the session cookie name.