class CRM_Core_Session

Class CRM_Core_Session.

Constants

USER_CONTEXT

Properties

static $_managedNames Cache of all the session names that we manage.
protected string $_key Key is used to allow the application to have multiple top level scopes rather than a single scope. (avoids naming conflicts). We also extend this idea further and have local scopes within a global scope. Allows us to do cool things like resetting a specific area of the session code while keeping the rest intact
protected object $_session This is just a reference to the real session. Allows us to debug this class a wee bit easier

Methods

__construct()

Constructor.

singleton()

Singleton function used to manage this object.

initialize(bool $isRead = FALSE)

Creates an array in the session.

reset(int $all = 1)

Resets the session store.

createScope(string $prefix, bool $isRead = FALSE)

Creates a session local scope.

resetScope(string $prefix)

Resets the session local scope.

set(string $name, mixed $value = NULL, string $prefix = NULL)

Store the variable with the value in the session scope.

mixed
get(string $name, string $prefix = NULL)

Gets the value of the named variable in the session scope.

getVars(array $vars, string $prefix = '')

Gets all the variables in the current session scope and stuffs them in an associate array.

mixed
timer(string $name, int $expire)

Set and check a timer.

pushUserContext(string $userContext, bool $check = TRUE)

Adds a userContext to the stack.

replaceUserContext(string $userContext)

Replace the userContext of the stack with the passed one.

string
popUserContext()

Pops the top userContext stack.

string
readUserContext()

Reads the top userContext stack.

debug(int $all = 1)

Dumps the session to the log.

string
getStatus(bool $reset = FALSE)

Fetches status messages.

static 
setStatus(string $text, string $title = '', string $type = 'alert', array $options = array())

Stores an alert to be displayed to the user via crm-messages.

static 
registerAndRetrieveSessionObjects(string|array $names)

Register and retrieve session objects.

static 
storeSessionObjects(bool $reset = TRUE)

Store session objects.

static int|NULL
getLoggedInContactID()

Retrieve contact id of the logged in user.

string
getLoggedInContactDisplayName()

Get display name of the logged in user.

bool
isEmpty()

Check if session is empty.

Details

at line 84
CRM_Core_Session __construct()

Constructor.

The CMS takes care of initiating the php session handler session_start().

When using CiviCRM standalone (w/o a CMS), we start the session in index.php and then pass it off to here.

All crm code should always use the session using CRM_Core_Session. we prefix stuff to avoid collisions with the CMS and also collisions with other crm modules!

This constructor is invoked whenever any module requests an instance of the session and one is not available.

Return Value

CRM_Core_Session

at line 93
static CRM_Core_Session singleton()

Singleton function used to manage this object.

Return Value

CRM_Core_Session

at line 108
initialize(bool $isRead = FALSE)

Creates an array in the session.

All variables now will be stored under this array.

Parameters

bool $isRead Is this a read operation, in this case, the session will not be touched.

at line 149
reset(int $all = 1)

Resets the session store.

Parameters

int $all

at line 171
createScope(string $prefix, bool $isRead = FALSE)

Creates a session local scope.

Parameters

string $prefix Local scope name.
bool $isRead Is this a read operation, in this case, the session will not be touched.

at line 189
resetScope(string $prefix)

Resets the session local scope.

Parameters

string $prefix Local scope name.

at line 217
set(string $name, mixed $value = NULL, string $prefix = NULL)

Store the variable with the value in the session scope.

This function takes a name, value pair and stores this in the session scope. Not sure what happens if we try to store complex objects in the session. I suspect it is supported but we need to verify this

Parameters

string $name Name of the variable.
mixed $value Value of the variable.
string $prefix A string to prefix the keys in the session with.

at line 252
mixed get(string $name, string $prefix = NULL)

Gets the value of the named variable in the session scope.

This function takes a name and retrieves the value of this variable from the session scope.

Parameters

string $name name of the variable.
string $prefix adds another level of scope to the session.

Return Value

mixed

at line 281
getVars(array $vars, string $prefix = '')

Gets all the variables in the current session scope and stuffs them in an associate array.

Parameters

array $vars Associative array to store name/value pairs.
string $prefix Will be stripped from the key before putting it in the return.

at line 316
mixed timer(string $name, int $expire)

Set and check a timer.

If it's expired, it will be set again.

Good for showing a message to the user every hour or day (so not bugging them on every page) Returns true-ish values if the timer is not set or expired, and false if the timer is still running If you want to get more nuanced, you can check the type of the return to see if it's 'not set' or actually expired at a certain time

Parameters

string $name name of the timer.
int $expire expiry time (in seconds).

Return Value

mixed

at line 333
pushUserContext(string $userContext, bool $check = TRUE)

Adds a userContext to the stack.

Parameters

string $userContext The url to return to when done.
bool $check Should we do a dupe checking with the top element.

at line 368
replaceUserContext(string $userContext)

Replace the userContext of the stack with the passed one.

Parameters

string $userContext The url to return to when done.

at line 385
string popUserContext()

Pops the top userContext stack.

Return Value

string the top of the userContext stack (also pops the top element)

at line 397
string readUserContext()

Reads the top userContext stack.

Return Value

string the top of the userContext stack

at line 410
debug(int $all = 1)

Dumps the session to the log.

Parameters

int $all

at line 429
string getStatus(bool $reset = FALSE)

Fetches status messages.

Parameters

bool $reset Should we reset the status variable?.

Return Value

string the status message if any

at line 469
static setStatus(string $text, string $title = '', string $type = 'alert', array $options = array())

Stores an alert to be displayed to the user via crm-messages.

Parameters

string $text The status message
string $title The optional title of this message
string $type The type of this message (printed as a css class). Possible options: - 'alert' (default) - 'info' - 'success' - 'error' (this message type by default will remain on the screen until the user dismisses it) - 'no-popup' (will display in the document like old-school)
array $options Additional options. Possible values: - 'unique' (default: true) Check if this message was already set before adding - 'expires' how long to display this message before fadeout (in ms) set to 0 for no expiration defaults to 10 seconds for most messages, 5 if it has a title but no body, or 0 for errors or messages containing links

at line 503
static registerAndRetrieveSessionObjects(string|array $names)

Register and retrieve session objects.

Parameters

string|array $names

at line 523
static storeSessionObjects(bool $reset = TRUE)

Store session objects.

Parameters

bool $reset

at line 541
static int|NULL getLoggedInContactID()

Retrieve contact id of the logged in user.

Return Value

int|NULL contact ID of logged in user

at line 556
string getLoggedInContactDisplayName()

Get display name of the logged in user.

Return Value

string

Exceptions

CiviCRM_API3_Exception

at line 571
bool isEmpty()

Check if session is empty.

if so we don't cache stuff that we can get away with, helps proxies like varnish.

Return Value

bool