Base session class.
- package
- Kohana
- category
- Session
- author
- Kohana Team
- copyright
- © 2008-2012 Kohana Team
- license
- http://kohanaframework.org/license
Class declared in SYSPATH/classes/session.php on line 3.
public static string
$defaultlink to this
default session adapter
string(6) "native"
public static array
$instanceslink to this
session instances
array(0)
session data
protected bool
$_destroyedlink to this
session destroyed?
protected bool
$_encryptedlink to this
encrypt session data?
cookie lifetime
cookie name
public __construct( [ array $config = NULL , string $id = NULL ] )
(defined in Kohana_Session)
link to this
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
Source Code
public function __construct( array $config = NULL, $id = NULL)
{
if (isset( $config [ 'name' ]))
{
$this ->_name = (string) $config [ 'name' ];
}
if (isset( $config [ 'lifetime' ]))
{
$this ->_lifetime = (int) $config [ 'lifetime' ];
}
if (isset( $config [ 'encrypted' ]))
{
if ( $config [ 'encrypted' ] === TRUE)
{
$config [ 'encrypted' ] = 'default' ;
}
$this ->_encrypted = $config [ 'encrypted' ];
}
$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 using base64_encode.
Tags
Return Values
Source Code
public function __toString()
{
$data = serialize( $this ->_data);
if ( $this ->_encrypted)
{
$data = Encrypt::instance( $this ->_encrypted)->encode( $data );
}
else
{
$data = base64_encode ( $data );
}
return $data ;
}
|
Returns the current session array. The returned array can also be
assigned by reference.
$data = $session ->as_array();
$data =& $session ->as_array();
|
Return Values
Source Code
public function & as_array()
{
return $this ->_data;
}
|
public bind( string $key , mixed & $value )
(defined in Kohana_Session)
link to this
Set a variable by reference.
$session ->bind( 'foo' , $foo );
|
Parameters
-
string
$key
required - Variable name
-
byref mixed
$value
required - Referenced value
Return Values
Source Code
public function bind( $key , & $value )
{
$this ->_data[ $key ] =& $value ;
return $this ;
}
|
Removes a variable in the session array.
Parameters
-
string
$key
required - ,... variable name
Return Values
Source Code
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();
|
Return Values
Source Code
public function destroy()
{
if ( $this ->_destroyed === FALSE)
{
if ( $this ->_destroyed = $this ->_destroy())
{
$this ->_data = array ();
}
}
return $this ->_destroyed;
}
|
public get( string $key [, mixed $default = NULL ] )
(defined in Kohana_Session)
link to this
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
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)
link to this
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
Source Code
public function get_once( $key , $default = NULL)
{
$value = $this ->get( $key , $default );
unset( $this ->_data[ $key ]);
return $value ;
}
|
Get the current session id, if the session supports it.
Not all session types have ids.
Tags
Return Values
Source Code
public function id()
{
return NULL;
}
|
public static instance( [ string $type = NULL , string $id = NULL ] )
(defined in Kohana_Session)
link to this
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
Source Code
public static function instance( $type = NULL, $id = NULL)
{
if ( $type === NULL)
{
$type = Session:: $default ;
}
if ( ! isset(Session:: $instances [ $type ]))
{
$config = Kohana:: $config ->load( 'session' )->get( $type );
$class = 'Session_' .ucfirst( $type );
Session:: $instances [ $type ] = $session = new $class ( $config , $id );
register_shutdown_function( array ( $session , 'write' ));
}
return Session:: $instances [ $type ];
}
|
Get the current session cookie name.
$name = $session ->name();
|
Tags
Return Values
Source Code
public function name()
{
return $this ->_name;
}
|
Loads existing session data.
Parameters
-
string
$id
= NULL - Session id
Return Values
Source Code
public function read( $id = NULL)
{
$data = NULL;
try
{
if ( is_string ( $data = $this ->_read( $id )))
{
if ( $this ->_encrypted)
{
$data = Encrypt::instance( $this ->_encrypted)->decode( $data );
}
else
{
$data = base64_decode ( $data );
}
$data = unserialize( $data );
}
else
{
}
}
catch (Exception $e )
{
throw new Session_Exception( 'Error reading session data.' , NULL, Session_Exception::SESSION_CORRUPT);
}
if ( is_array ( $data ))
{
$this ->_data = $data ;
}
}
|
Generates a new session id and returns it.
$id = $session ->regenerate();
|
Return Values
Source Code
public function regenerate()
{
return $this ->_regenerate();
}
|
Restart the session.
$success = $session ->restart();
|
Return Values
Source Code
public function restart()
{
if ( $this ->_destroyed === FALSE)
{
$this ->destroy();
}
$this ->_destroyed = FALSE;
return $this ->_restart();
}
|
public set( string $key , mixed $value )
(defined in Kohana_Session)
link to this
Set a variable in the session array.
$session ->set( 'foo' , 'bar' );
|
Parameters
-
string
$key
required - Variable name
-
mixed
$value
required - Value
Return Values
Source Code
public function set( $key , $value )
{
$this ->_data[ $key ] = $value ;
return $this ;
}
|
Sets the last_active timestamp and saves the session.
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
Source Code
public function write()
{
if (headers_sent() OR $this ->_destroyed)
{
return FALSE;
}
$this ->_data[ 'last_active' ] = time();
try
{
return $this ->_write();
}
catch (Exception $e )
{
Kohana:: $log ->add(Log::ERROR, Kohana_Exception::text( $e ))->write();
return FALSE;
}
}
|
Destroys the current session.
Return Values
Source Code
abstract protected function _destroy();
|
abstract protected _read( [ string $id = NULL ] )
(defined in Kohana_Session)
link to this
Loads the raw session data string and returns it.
Parameters
-
string
$id
= NULL - Session id
Return Values
Source Code
abstract protected function _read( $id = NULL);
|
Generate a new session id and return it.
Return Values
Source Code
abstract protected function _regenerate();
|
Restarts the current session.
Return Values
Source Code
abstract protected function _restart();
|
Writes the current session.
Return Values
Source Code
abstract protected function _write();
|