Session_Native
» Kohana_Session_Native
» Session
» Kohana_Session
Native PHP session class.
Methods
protected _read ( string $id = NULL )
Loads the raw session data string and returns it.
Returns:
string
Source:
protected function _read($id = NULL)
{
// Set the cookie lifetime
session_set_cookie_params($this->_lifetime);
// 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;
}
protected _regenerate ( )
Generate a new session id and return it.
Returns:
string
Source:
protected function _regenerate()
{
// Regenerate the session id
session_regenerate_id();
return session_id();
}
protected _write ( )
Writes the current session.
Returns:
boolean
Source:
protected function _write()
{
// Write and close the session
session_write_close();
return TRUE;
}
protected _destroy ( )
Destroys the current session.
Returns:
boolean
Source:
protected function _destroy()
{
// Destroy the current session
session_destroy();
return ! session_id();
}
public static instance ( string $type = 'native', string $id = NULL )
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.
Returns:
Session
Source:
public static function instance($type = 'native', $id = NULL)
{
if ( ! isset(Session::$instances[$type]))
{
// Load the configuration for this type
$config = Kohana::config('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(array($session, 'write'));
}
return Session::$instances[$type];
}
protected __construct ( array $config = NULL, string $id = NULL )
Overloads the name, lifetime, and encrypted session settings.
Returns:
void
Source:
protected 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 ( )
Session object is rendered to a serialized string.
Returns:
string
Source:
public function __toString()
{
// Serialize the data array
$data = serialize($this->_data);
if ($this->_encrypted)
{
// Encrypt the data using the default key
$data = Encrypt::instance($this->_encrypted)->encode($data);
}
else
{
// Obfuscate the data with base64 encoding
$data = base64_encode($data);
}
return $data;
}
public as_array ( )
Returns the current session array.
Returns:
array
Source:
public function & as_array()
{
return $this->_data;
}
public get ( string $key, mixed $default = NULL )
Get a variable from the session array.
Returns:
mixed
Source:
public function get($key, $default = NULL)
{
return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}
public set ( string $key, mixed $value )
Set a variable in the session array.
Returns:
Session
Source:
public function set($key, $value)
{
$this->_data[$key] = $value;
return $this;
}
public delete ( string $key )
Removes a variable in the session array.
Returns:
Session
Source:
public function delete($key)
{
unset($this->_data[$key]);
return $this;
}
public read ( string $id = NULL )
Loads the session data.
Returns:
void
Source:
public function read($id = NULL)
{
if (is_string($data = $this->_read($id)))
{
try
{
if ($this->_encrypted)
{
// Decrypt the data using the default key
$data = Encrypt::instance($this->_encrypted)->decode($data);
}
else
{
// Decode the base64 encoded data
$data = base64_decode($data);
}
// Unserialize the data
$data = unserialize($data);
}
catch (Exception $e)
{
// Ignore all reading errors
}
}
if (is_array($data))
{
// Load the data locally
$this->_data = $data;
}
}
public regenerate ( )
Generates a new session id and returns it.
Returns:
string
Source:
public function regenerate()
{
return $this->_regenerate();
}
public write ( )
Sets the last_active timestamp and saves the session.
Returns:
boolean
Source:
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();
return $this->_write();
}
public destroy ( )
Destroy the current session.
Returns:
boolean
Source:
public function destroy()
{
if ($this->_destroyed === FALSE)
{
if ($this->_destroyed = $this->_destroy())
{
// The session has been destroyed, clear all data
$this->_data = array();
}
}
return $this->_destroyed;
}