Class Session

Session

extends Kohana_Session

Base session class.

package
Kohana
category
Session
author
Kohana Team
copyright
(c) Kohana Team
license
https://koseven.ga/LICENSE.md


Information

This class is a transparent base class for Kohana_Session

Properties

public static string $default

default session adapter

string(6) "native"

public static array $instances

session instances

array(0) 

protected array $_data

session data

Default value:
array(0) 

protected bool $_destroyed

session destroyed?

Default value:
bool FALSE

protected bool $_encrypted

encrypt session data?

Default value:
bool FALSE

protected int $_lifetime

cookie lifetime

Default value:
integer 0

protected string $_name

cookie name

Default value:
string(7) "session"

Methods

public __construct([ array $config = NULL , string $id = NULL ] ) (defined in Kohana_Session)

Overloads the name, lifetime, and encrypted session settings.

Sessions can only be created using the Session::instance method.

Parameters

  • array $config = NULL - Configuration
  • string $id = NULL - Session id

Tags

Return Values

  • void

Source Code

public function __construct(array $config = NULL, $id = NULL)
{
	if (isset($config['name']))
	{
		// Cookie name to store the session id in
		$this->_name = (string) $config['name'];
	}

	if (isset($config['lifetime']))
	{
		// Cookie lifetime
		$this->_lifetime = (int) $config['lifetime'];
	}

	if (isset($config['encrypted']))
	{
		if ($config['encrypted'] === TRUE)
		{
			// Use the default Encrypt instance
			$config['encrypted'] = 'default';
		}

		// Enable or disable encryption of data
		$this->_encrypted = $config['encrypted'];
	}

	// Load the session
	$this->read($id);
}

public __toString() (defined in Kohana_Session)

Session object is rendered to a serialized string. If encryption is enabled, the session will be encrypted. If not, the output string will be encoded.

echo $session;

Tags

Return Values

  • string

Source Code

public function __toString()
{
	// Serialize the data array
	$data = $this->_serialize($this->_data);

	if ($this->_encrypted)
	{
		// Encrypt the data using the default key
		$data = Encrypt::instance($this->_encrypted)->encode($data);
	}
	else
	{
		// Encode the data
		$data = $this->_encode($data);
	}

	return $data;
}

public as_array() (defined in Kohana_Session)

Returns the current session array. The returned array can also be assigned by reference.

// Get a copy of the current session data
$data = $session->as_array();

// Assign by reference for modification
$data =& $session->as_array();

Return Values

  • array

Source Code

public function & as_array()
{
	return $this->_data;
}

public bind(string $key , mixed & $value ) (defined in Kohana_Session)

Set a variable by reference.

$session->bind('foo', $foo);

Parameters

  • string $key required - Variable name
  • byref mixed $value required - Referenced value

Return Values

  • $this

Source Code

public function bind($key, & $value)
{
	$this->_data[$key] =& $value;

	return $this;
}

public delete(string $key ) (defined in Kohana_Session)

Removes a variable in the session array.

$session->delete('foo');

Parameters

  • string $key required - ,... variable name

Return Values

  • $this

Source Code

public function delete($key)
{
	$args = func_get_args();

	foreach ($args as $key)
	{
		unset($this->_data[$key]);
	}

	return $this;
}

public destroy() (defined in Kohana_Session)

Completely destroy the current session.

$success = $session->destroy();

Return Values

  • boolean

Source Code

public function destroy()
{
	if ($this->_destroyed === FALSE)
	{
		if ($this->_destroyed = $this->_destroy())
		{
			// The session has been destroyed, clear all data
			$this->_data = [];
		}
	}

	return $this->_destroyed;
}

public get(string $key [, mixed $default = NULL ] ) (defined in Kohana_Session)

Get a variable from the session array.

$foo = $session->get('foo');

Parameters

  • string $key required - Variable name
  • mixed $default = NULL - Default value to return

Return Values

  • mixed

Source Code

public function get($key, $default = NULL)
{
	return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}

public get_once(string $key [, mixed $default = NULL ] ) (defined in Kohana_Session)

Get and delete a variable from the session array.

$bar = $session->get_once('bar');

Parameters

  • string $key required - Variable name
  • mixed $default = NULL - Default value to return

Return Values

  • mixed

Source Code

public function get_once($key, $default = NULL)
{
	$value = $this->get($key, $default);

	unset($this->_data[$key]);

	return $value;
}

public id() (defined in Kohana_Session)

Get the current session id, if the session supports it.

$id = $session->id();

Not all session types have ids.

Tags

  • Since - 3.0.8

Return Values

  • string

Source Code

public function id()
{
	return NULL;
}

public static instance([ string $type = NULL , string $id = NULL ] ) (defined in Kohana_Session)

Creates a singleton session of the given type. Some session types (native, database) also support restarting a session by passing a session id as the second parameter.

$session = Session::instance();

Session::write will automatically be called when the request ends.

Parameters

  • string $type = NULL - Type of session (native, cookie, etc)
  • string $id = NULL - Session identifier

Tags

Return Values

  • Session

Source Code

public static function instance($type = NULL, $id = NULL)
{
	if ($type === NULL)
	{
		// Use the default type
		$type = Session::$default;
	}

	if ( ! isset(Session::$instances[$type]))
	{
		// Load the configuration for this type
		$config = Kohana::$config->load('session')->get($type);

		// Set the session class name
		$class = 'Session_'.ucfirst($type);

		// Create a new session instance
		Session::$instances[$type] = $session = new $class($config, $id);

		// Write the session at shutdown
		register_shutdown_function([$session, 'write']);
	}

	return Session::$instances[$type];
}

public name() (defined in Kohana_Session)

Get the current session cookie name.

$name = $session->name();

Tags

  • Since - 3.0.8

Return Values

  • string

Source Code

public function name()
{
	return $this->_name;
}

public read([ string $id = NULL ] ) (defined in Kohana_Session)

Loads existing session data.

$session->read();

Parameters

  • string $id = NULL - Session id

Return Values

  • void

Source Code

public function read($id = NULL)
{
	$data = NULL;

	try
	{
		if (is_string($data = $this->_read($id)))
		{
			if ($this->_encrypted)
			{
				// Decrypt the data using the default key
				$data = Encrypt::instance($this->_encrypted)->decode($data);
			}
			else
			{
				// Decode the data
				$data = $this->_decode($data);
			}

			// Unserialize the data
			$data = $this->_unserialize($data);
		}
		else
		{
			// Ignore these, session is valid, likely no data though.
		}
	}
	catch (Exception $e)
	{
		// Error reading the session, usually a corrupt session.
		throw new Session_Exception('Error reading session data.', NULL, Session_Exception::SESSION_CORRUPT);
	}

	if (is_array($data))
	{
		// Load the data locally
		$this->_data = $data;
	}
}

public regenerate() (defined in Kohana_Session)

Generates a new session id and returns it.

$id = $session->regenerate();

Return Values

  • string

Source Code

public function regenerate()
{
	return $this->_regenerate();
}

public restart() (defined in Kohana_Session)

Restart the session.

$success = $session->restart();

Return Values

  • boolean

Source Code

public function restart()
{
	if ($this->_destroyed === FALSE)
	{
		// Wipe out the current session.
		$this->destroy();
	}

	// Allow the new session to be saved
	$this->_destroyed = FALSE;

	return $this->_restart();
}

public set(string $key , mixed $value ) (defined in Kohana_Session)

Set a variable in the session array.

$session->set('foo', 'bar');

Parameters

  • string $key required - Variable name
  • mixed $value required - Value

Return Values

  • $this

Source Code

public function set($key, $value)
{
	$this->_data[$key] = $value;

	return $this;
}

public write() (defined in Kohana_Session)

Sets the last_active timestamp and saves the session.

$session->write();

Any errors that occur during session writing will be logged, but not displayed, because sessions are written after output has been sent.

Tags

Return Values

  • boolean

Source Code

public function write()
{
	if (headers_sent() OR $this->_destroyed)
	{
		// Session cannot be written when the headers are sent or when
		// the session has been destroyed
		return FALSE;
	}

	// Set the last active timestamp
	$this->_data['last_active'] = time();

	try
	{
		return $this->_write();
	}
	catch (Exception $e)
	{
		// Log & ignore all errors when a write fails
		Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e))->write();

		return FALSE;
	}
}

protected _decode(string $data ) (defined in Kohana_Session)

Decodes the session data using base64_decode.

Parameters

  • string $data required - Data

Return Values

  • string

Source Code

protected function _decode($data)
{
	return base64_decode($data);
}

abstract protected _destroy() (defined in Kohana_Session)

Destroys the current session.

Return Values

  • boolean

Source Code

abstract protected function _destroy();

protected _encode(string $data ) (defined in Kohana_Session)

Encodes the session data using base64_encode.

Parameters

  • string $data required - Data

Return Values

  • string

Source Code

protected function _encode($data)
{
	return base64_encode($data);
}

abstract protected _read([ string $id = NULL ] ) (defined in Kohana_Session)

Loads the raw session data string and returns it.

Parameters

  • string $id = NULL - Session id

Return Values

  • string

Source Code

abstract protected function _read($id = NULL);

abstract protected _regenerate() (defined in Kohana_Session)

Generate a new session id and return it.

Return Values

  • string

Source Code

abstract protected function _regenerate();

abstract protected _restart() (defined in Kohana_Session)

Restarts the current session.

Return Values

  • boolean

Source Code

abstract protected function _restart();

protected _serialize(array $data ) (defined in Kohana_Session)

Serializes the session data.

Parameters

  • array $data required - Data

Return Values

  • string

Source Code

protected function _serialize($data)
{
	return serialize($data);
}

protected _unserialize(string $data ) (defined in Kohana_Session)

Unserializes the session data.

Parameters

  • string $data required - Data

Return Values

  • array

Source Code

protected function _unserialize($data)
{
	return unserialize($data);
}

abstract protected _write() (defined in Kohana_Session)

Writes the current session.

Return Values

  • boolean

Source Code

abstract protected function _write();

Do you want to contribute to Koseven?

We need YOUR help!

This project is open source. What does this mean? YOU can help:
  • Found a bug? Report it on Github
  • Need a feature? Add it Here
  • Want to help? Join the Forum
Go to Github