This class is a transparent base class for Session_Native and should not be accessed directly.
Native PHP session class.
Class declared in SYSPATH/classes/Kohana/Session/Native.php on line 12.
string
$defaultdefault session adapter
string(6) "native"
array
$instancessession instances
array(0)
array
$_datasession data
array(0)
bool
$_destroyedsession destroyed?
bool FALSE
bool
$_encryptedencrypt session data?
bool FALSE
int
$_lifetimecookie lifetime
integer 0
string
$_namecookie name
string(7) "session"
string
public function id()
{
return session_id();
}
Overloads the name, lifetime, and encrypted session settings.
Sessions can only be created using the Session::instance method.
array
$config
= NULL - Configuration string
$id
= NULL - Session id void
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);
}
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;
string
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;
}
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();
array
public function & as_array()
{
return $this->_data;
}
Set a variable by reference.
$session->bind('foo', $foo);
string
$key
required - Variable name byref mixed
$value
required - Referenced value $this
public function bind($key, & $value)
{
$this->_data[$key] = & $value;
return $this;
}
Removes a variable in the session array.
$session->delete('foo');
string
$key
required - ,... variable name $this
public function delete($key)
{
$args = func_get_args();
foreach ($args as $key) {
unset($this->_data[$key]);
}
return $this;
}
Completely destroy the current session.
$success = $session->destroy();
boolean
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;
}
Get a variable from the session array.
$foo = $session->get('foo');
string
$key
required - Variable name mixed
$default
= NULL - Default value to return mixed
public function get($key, $default = null)
{
return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}
Get and delete a variable from the session array.
$bar = $session->get_once('bar');
string
$key
required - Variable name mixed
$default
= NULL - Default value to return mixed
public function get_once($key, $default = null)
{
$value = $this->get($key, $default);
unset($this->_data[$key]);
return $value;
}
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.
string
$type
= NULL - Type of session (native, cookie, etc) string
$id
= NULL - Session identifier Session
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];
}
Get the current session cookie name.
$name = $session->name();
string
public function name()
{
return $this->_name;
}
Loads existing session data.
$session->read();
string
$id
= NULL - Session id void
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;
}
}
Generates a new session id and returns it.
$id = $session->regenerate();
string
public function regenerate()
{
return $this->_regenerate();
}
Restart the session.
$success = $session->restart();
boolean
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();
}
Set a variable in the session array.
$session->set('foo', 'bar');
string
$key
required - Variable name mixed
$value
required - Value $this
public function set($key, $value)
{
$this->_data[$key] = $value;
return $this;
}
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.
boolean
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;
}
}
bool
protected function _destroy()
{
// Destroy the current session
session_destroy();
// Did destruction work?
$status = !session_id();
if ($status) {
// Make sure the session cannot be restarted
Cookie::delete($this->_name);
}
return $status;
}
string
$id
= NULL - Session id null
protected function _read($id = null)
{
/**
* session_set_cookie_params will override php ini settings
* If Cookie::$domain is null or empty and is passed, PHP
* will override ini and sent cookies with the host name
* of the server which generated the cookie
*
* see issue #3604
*
* see http://www.php.net/manual/en/function.session-set-cookie-params.php
* see http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
*
* set to Cookie::$domain if available, otherwise default to ini setting
*/
$session_cookie_domain = empty(Cookie::$domain) ? ini_get('session.cookie_domain') : Cookie::$domain;
// Sync up the session cookie with Cookie parameters
session_set_cookie_params(
$this->_lifetime, Cookie::$path, $session_cookie_domain, Cookie::$secure, Cookie::$httponly
);
// Do not allow PHP to send Cache-Control headers
session_cache_limiter(false);
// Set the session cookie name
session_name($this->_name);
if ($id) {
// Set the session id
session_id($id);
}
// Start the session
session_start();
// Use the $_SESSION global for storing data
$this->_data = & $_SESSION;
return null;
}
string
protected function _regenerate()
{
// Regenerate the session id
session_regenerate_id();
return session_id();
}
bool
protected function _restart()
{
// Fire up a new session
$status = session_start();
// Use the $_SESSION global for storing data
$this->_data = & $_SESSION;
return $status;
}
bool
protected function _write()
{
// Write and close the session
session_write_close();
return true;
}
Decodes the session data using base64_decode.
string
$data
required - Data string
protected function _decode($data)
{
return base64_decode($data);
}
Encodes the session data using base64_encode.
string
$data
required - Data string
protected function _encode($data)
{
return base64_encode($data);
}
Serializes the session data.
array
$data
required - Data string
protected function _serialize($data)
{
return serialize($data);
}
Unserializes the session data.
string
$data
required - Data array
protected function _unserialize($data)
{
return unserialize($data);
}