Class CakeSession

Session class for CakePHP.

CakePHP abstracts the handling of sessions. There are several convenient methods to access session information. This class is the implementation of those methods. They are mostly used by the Session Component.

Package: Cake\Model\Datasource
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Model/Datasource/CakeSession.php

Properties summary

  • $_cookieName protected static
    string
    Session cookie name
  • $_initialized protected static
    boolean
    Whether or not the init function in this class was already called
  • $_userAgent protected static
    string
    User agent string
  • $cookieLifeTime public static
    integer
    Cookie lifetime
  • $error public static
    array
    Error messages for this session
  • $host public static
    string
    Hostname
  • $id public static
    string
    Current Session id
  • $lastError public static
    integer
    Error number of last occurred error
  • $path public static
    string
    Path to where the session is active.
  • $requestCountdown public static
    integer

    Number of requests that can occur during a session time without the session being renewed. This feature is only used when config value Session.autoRegenerate is set to true.

  • $sessionTime public static
    integer
    Time when this session becomes invalid.
  • $time public static
    integer
    Start time for this session.
  • $timeout public static
    integer
    Session timeout multiplier factor
  • $valid public static
    boolean
    True if the Session is still valid

Method Summary

  • _checkValid() protected static
    Helper method to create a new session.
  • _configureSession() protected static
    Helper method to initialize a session, based on CakePHP core settings.
  • _cookieName() protected static
    Get session cookie name.
  • _defaultConfig() protected static
    Get one of the prebaked default session configurations.
  • _error() protected static
    Return error description for given error number.
  • _getHandler() protected static
    Find the handler class and make sure it implements the correct interface.
  • _hasSession() protected static
    Returns whether a session exists
  • _overwrite() protected static
    Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
  • _returnSessionVars() protected static
    Returns all session variables.
  • _setError() protected static
    Helper method to set an internal error message.
  • _setHost() protected static
    Set the host name
  • _setPath() protected static
    Setup the Path variable
  • _startSession() protected static
    Helper method to start a session
  • _validAgentAndTime() protected static

    Tests that the user agent is valid and that the session hasn't 'timed out'. Since timeouts are implemented in CakeSession it checks the current static::$time against the time the session is set to expire. The User agent is only checked if Session.checkAgent == true.

  • _writeConfig() protected static
    Writes configuration variables to the session
  • check() public static
    Returns true if given variable is set in session.
  • clear() public static
    Clears the session.
  • consume() public static
    Reads and deletes a variable from session.
  • delete() public static
    Removes a variable from session.
  • destroy() public static
    Helper method to destroy invalid sessions.
  • error() public static
    Returns last occurred error as a string, if any.
  • id() public static

    Returns the session id. Calling this method will not auto start the session. You might have to manually assert a started session.

  • init() public static
    Pseudo constructor.
  • read() public static
    Returns given session variable, or all of them, if no parameters given.
  • renew() public static
    Restarts this session.
  • start() public static
    Starts the Session.
  • started() public static
    Determine if Session has been started.
  • userAgent() public static
    Get / Set the user agent
  • valid() public static
    Returns true if session is valid.
  • write() public static
    Writes value to given session variable name.

Method Detail

_checkValid()source protected static

_checkValid( )

Helper method to create a new session.

_configureSession()source protected static

_configureSession( )

Helper method to initialize a session, based on CakePHP core settings.

Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.

Throws

CakeSessionException
Throws exceptions when ini_set() fails.

_cookieName()source protected static

_cookieName( )

Get session cookie name.

Returns

string

_defaultConfig()source protected static

_defaultConfig( string $name )

Get one of the prebaked default session configurations.

Parameters

string $name
Config name.

Returns

boolean|array

_error()source protected static

_error( integer $errorNumber )

Return error description for given error number.

Parameters

integer $errorNumber
Error to set

Returns

string
Error as string

_getHandler()source protected static

_getHandler( string $handler )

Find the handler class and make sure it implements the correct interface.

Parameters

string $handler
Handler name.

Throws

CakeSessionException

_hasSession()source protected static

_hasSession( )

Returns whether a session exists

Returns

boolean

_overwrite()source protected static

_overwrite( array $old , array $new )

Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.

Parameters

array $old
$old Set of old variables => values
array $new
New set of variable => value

_returnSessionVars()source protected static

_returnSessionVars( )

Returns all session variables.

Returns

mixed
Full $_SESSION array, or false on error.

_setError()source protected static

_setError( integer $errorNumber , string $errorMessage )

Helper method to set an internal error message.

Parameters

integer $errorNumber
Number of the error
string $errorMessage
Description of the error

_setHost()source protected static

_setHost( string $host )

Set the host name

Parameters

string $host
Hostname

_setPath()source protected static

_setPath( string|null $base null )

Setup the Path variable

Parameters

string|null $base optional null
base path

_startSession()source protected static

_startSession( )

Helper method to start a session

Returns

boolean
Success

_validAgentAndTime()source protected static

_validAgentAndTime( )

Tests that the user agent is valid and that the session hasn't 'timed out'. Since timeouts are implemented in CakeSession it checks the current static::$time against the time the session is set to expire. The User agent is only checked if Session.checkAgent == true.

Returns

boolean

_writeConfig()source protected static

_writeConfig( )

Writes configuration variables to the session

check()source public static

check( string $name )

Returns true if given variable is set in session.

Parameters

string $name
Variable name to check for

Returns

boolean
True if variable is there

clear()source public static

clear( boolean $renew true )

Clears the session.

Optionally also clears the session id and renews the session.

Parameters

boolean $renew optional true
If the session should also be renewed. Defaults to true.

consume()source public static

consume( string $name )

Reads and deletes a variable from session.

Parameters

string $name
The key to read and remove (or a path as sent to Hash.extract).

Returns

mixed

The value of the session variable, null if session not available, session not started, or provided name not found in the session.


delete()source public static

delete( string $name )

Removes a variable from session.

Parameters

string $name
Session variable to remove

Returns

boolean
Success

destroy()source public static

destroy( )

Helper method to destroy invalid sessions.

error()source public static

error( )

Returns last occurred error as a string, if any.

Returns

mixed
Error description as a string, or false.

id()source public static

id( string|null $id null )

Returns the session id. Calling this method will not auto start the session. You might have to manually assert a started session.

Passing an id into it, you can also replace the session id if the session has not already been started. Note that depending on the session handler, not all characters are allowed within the session id. For example, the file session handler only allows characters in the range a-z A-Z 0-9 , (comma) and - (minus).

Parameters

string|null $id optional null
Id to replace the current session id

Returns

string
Session id

init()source public static

init( string|null $base null )

Pseudo constructor.

Parameters

string|null $base optional null
The base path for the Session

read()source public static

read( string|null $name null )

Returns given session variable, or all of them, if no parameters given.

Parameters

string|null $name optional null
The name of the session variable (or a path as sent to Set.extract)

Returns

mixed

The value of the session variable, null if session not available, session not started, or provided name not found in the session, false on failure.


renew()source public static

renew( )

Restarts this session.

start()source public static

start( )

Starts the Session.

Returns

boolean
True if session was started

started()source public static

started( )

Determine if Session has been started.

Returns

boolean
True if session has been started.

userAgent()source public static

userAgent( string|null $userAgent null )

Get / Set the user agent

Parameters

string|null $userAgent optional null
Set the user agent

Returns

string
Current user agent.

valid()source public static

valid( )

Returns true if session is valid.

Returns

boolean
Success

write()source public static

write( string|array $name , string $value null )

Writes value to given session variable name.

Parameters

string|array $name
Name of variable
string $value optional null
Value to write

Returns

boolean
True if the write was successful, false if the write failed

Properties detail

$_cookieNamesource

protected static string

Session cookie name

null

$_initializedsource

protected static boolean

Whether or not the init function in this class was already called

false

$_userAgentsource

protected static string

User agent string

''

$cookieLifeTimesource

public static integer

Cookie lifetime

$errorsource

public static array

Error messages for this session

false

$hostsource

public static string

Hostname

null

$idsource

public static string

Current Session id

null

$lastErrorsource

public static integer

Error number of last occurred error

null

$pathsource

public static string

Path to where the session is active.

'/'

$requestCountdownsource

public static integer

Number of requests that can occur during a session time without the session being renewed. This feature is only used when config value Session.autoRegenerate is set to true.

See

CakeSession::_checkValid()
10

$sessionTimesource

public static integer

Time when this session becomes invalid.

false

$timesource

public static integer

Start time for this session.

false

$timeoutsource

public static integer

Session timeout multiplier factor

null

$validsource

public static boolean

True if the Session is still valid

false

© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.8/class-CakeSession.html