Skip to content

Commit

Permalink
Merge pull request #819 from thechriswalker/dev
Browse files Browse the repository at this point in the history
Allowing Session id to be set manually (squash of #818)
  • Loading branch information
nateabele committed Feb 8, 2013
2 parents 583a0c4 + e00c63a commit f2e740c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
9 changes: 5 additions & 4 deletions storage/Session.php
Expand Up @@ -55,14 +55,15 @@ class Session extends \lithium\core\Adaptable {
protected static $_strategies = 'strategy.storage.session';

/**
* Returns the key used to identify the session.
* Returns (and Sets) the key used to identify the session.
*
* @param mixed $name Optional named session configuration.
* @param mixed $session_id Optional session id to use for this session.
* @return string Returns the value of the session identifier key, or `null` if no named
* configuration exists, or no session has been started.
* configuration exists, no session id has been set or no session has been started.
*/
public static function key($name = null) {
return is_object($adapter = static::adapter($name)) ? $adapter->key() : null;
public static function key($name = null, $sessionId = null) {
return is_object($adapter = static::adapter($name)) ? $adapter->key($sessionId) : null;
}

/**
Expand Down
25 changes: 15 additions & 10 deletions storage/session/adapter/Php.php
Expand Up @@ -77,23 +77,25 @@ protected function _init() {
* false otherwise.
*/
protected static function _start() {
if (session_id()) {
if (static::isStarted()) {
return true;
}
if (!isset($_SESSION)) {
session_cache_limiter('nocache');
}
session_cache_limiter('nocache');
return session_start();
}

/**
* Obtain the status of the session.
*
* @return boolean True if $_SESSION is accessible and if a '_timestamp' key
* has been set, false otherwise.
* @return boolean True if a session is currently started, False otherwise. If PHP5.4
* then we know, if PHP5.3 then we cannot tell for sure if a session
* has been closed.
*/
public static function isStarted() {
return (boolean) session_id();
if (function_exists("session_status")) {
return session_status() === PHP_SESSION_ACTIVE;
}
return isset($_SESSION) && session_id();
}

/**
Expand All @@ -103,7 +105,7 @@ public static function isStarted() {
* @return mixed Session ID, or `null` if the session has not been started.
*/
public static function key($key = null) {
if ($key) {
if ($key !== null) {
return session_id($key);
}
return session_id() ?: null;
Expand Down Expand Up @@ -217,10 +219,13 @@ public function clear(array $options = array()) {
/**
* Determines if PHP sessions are enabled.
*
* @return boolean True if enabled (that is, if session_id() returns a value), false otherwise.
* @return boolean True if enabled (php session functionality can be disabled completely), false otherwise
*/
public static function enabled() {
return (boolean) session_id();
if (function_exists("session_status")) {
return session_status() !== PHP_SESSION_DISABLED;
}
return in_array('session', get_loaded_extensions());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/cases/storage/session/adapter/PhpTest.php
Expand Up @@ -51,8 +51,9 @@ protected function _destroySession($name = null) {

public function testEnabled() {
$php = $this->php;
$this->_destroySession(session_name());
$this->assertFalse($php::enabled());
/* Is PHP Session support enabled? */
$sessionsSupported = in_array('session', get_loaded_extensions());
$this->assertEqual($sessionsSupported, $php::enabled());
}

public function testInit() {
Expand Down

0 comments on commit f2e740c

Please sign in to comment.