Native PHP 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/Native.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
- Default value:
array(0)
protected bool
$_destroyedlink to this
session destroyed?
- Default value:
bool FALSE
protected bool
$_encryptedlink to this
encrypt session data?
- Default value:
bool FALSE
cookie lifetime
- Default value:
integer 0
cookie name
- Default value:
string(7) "session"
Return Values
Source Code
public function id()
{
return session_id();
}
|
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.
Tags
Return Values
Source Code
public function __toString()
{
$data = $this ->_serialize( $this ->_data);
if ( $this ->_encrypted)
{
$data = Encrypt::instance( $this ->_encrypted)->encode( $data );
}
else
{
$data = $this ->_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 ;
}
|
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 = $this ->_decode( $data );
}
$data = $this ->_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;
}
}
|
Return Values
Source Code
protected function _destroy()
{
session_destroy();
$status = ! session_id();
if ( $status )
{
Cookie:: delete ( $this ->_name);
}
return $status ;
}
|
Parameters
-
string
$id
= NULL - Session id
Return Values
Source Code
protected function _read( $id = NULL)
{
$session_cookie_domain = empty (Cookie:: $domain )
? ini_get ( 'session.cookie_domain' )
: Cookie:: $domain ;
session_set_cookie_params(
$this ->_lifetime,
Cookie:: $path ,
$session_cookie_domain ,
Cookie:: $secure ,
Cookie:: $httponly
);
session_cache_limiter(FALSE);
session_name( $this ->_name);
if ( $id )
{
session_id( $id );
}
session_start();
$this ->_data =& $_SESSION ;
return NULL;
}
|
Return Values
Source Code
protected function _regenerate()
{
session_regenerate_id();
return session_id();
}
|
Return Values
Source Code
protected function _restart()
{
$status = session_start();
$this ->_data =& $_SESSION ;
return $status ;
}
|
Return Values
Source Code
protected function _write()
{
session_write_close();
return TRUE;
}
|
Parameters
-
string
$data
required - Data
Return Values
Source Code
protected function _decode( $data )
{
return base64_decode ( $data );
}
|
Parameters
-
string
$data
required - Data
Return Values
Source Code
protected function _encode( $data )
{
return base64_encode ( $data );
}
|
Serializes the session data.
Parameters
-
array
$data
required - Data
Return Values
Source Code
protected function _serialize( $data )
{
return serialize( $data );
}
|
protected _unserialize( string $data )
(defined in Kohana_Session)
link to this
Unserializes the session data.
Parameters
-
string
$data
required - Data
Return Values
Source Code
protected function _unserialize( $data )
{
return unserialize( $data );
}
|