PHP Class Habari\Session

Manages sessions for the PHP session routines
Inheritance: extends Singleton
Show file Open project: habari/system Class Usage Examples

Public Properties

Property Type Description
$session_changed
$session_id
$stored_session_hash
$write_queue * The initial data. Used to determine whether we should write anything.

Public Methods

Method Description
add_to_set ( string $set, mixed $value, string $key = null ) Adds a value to a session set
cache_limiter ( string $type = 'nocache' ) : void Sends HTTP headers that limit the cacheability of the page.
changed ( ) : boolean Determine if the session data has changed
clear_userid ( integer $user_id ) Clear the user_id attached to sessions, delete other sessions that are associated to the user_id
cookie ( string $data, null | integer $expiration = null ) Sends the Habari session cookie to the browser with the given payload and expiration.
create ( ) Create a new session - that is, generate an ID and set the session cookie
destroy ( ) Destroy stored session data by session id
error ( string $error, string $key = null ) Store an error message in the user's session
gc ( ) Session garbage collection deletes expired sessions
get_error ( string $key, boolean $clear = true ) : string Retrieve a specific error from stored errors.
get_errors ( boolean $clear = true ) : array Get all error messages from the user session
get_notice ( string $key, boolean $clear = true ) : string Retrieve a specific notice from stored errors.
get_notices ( boolean $clear = true ) : array Get all notice messages from the user session
get_set ( string $set, boolean $clear = true ) : array Return a set of messages
has_errors ( string $key = null ) : boolean Determine if there are error messages to display
has_messages ( ) : boolean Determine if there are messages that should be displayed Messages are not cleared when calling this function.
init ( ) Initialize the session handlers
messages_get ( boolean $clear = true, array $callback = null ) : mixed Return output of notice and error messages
messages_out ( boolean $clear = true, null | callable $callback = null ) Output notice and error messages
notice ( string $notice, string $key = null ) Store a notice message in the user's session
process_queue ( ) Call update() on QueryRecords or execute the function queued with self::queue()
queue ( QueryRecord | Callable $fn, null | string $name = null ) Add objects or functions to the post-processing queue, executed via self::process_queue()
read ( ) : boolean Read session data from the database and load it into the $_SESSION global.
remove_error ( string $key ) : boolean Removes a specific error from the stored errors.
remove_notice ( string $key ) : boolean Removes a specific notice from the stored notices.
session_data_hash ( ) : string Get a hash of the contents of the session data for change comparison
set_userid ( integer $user_id ) Sets the user_id attached to the current session
shutdown ( )
write ( ) Commit $_SESSION data to the database for this user.

Protected Methods

Method Description
get_subnet ( string $remote_address = '' ) : integer | string Helper function to find the Class A, B, or C subnet of the given IP address.

Method Details

add_to_set() public static method

Adds a value to a session set
public static add_to_set ( string $set, mixed $value, string $key = null )
$set string Name of the set
$value mixed value to store
$key string Optional unique key for the set under which to store the value

cache_limiter() public static method

Sends HTTP headers that limit the cacheability of the page.
public static cache_limiter ( string $type = 'nocache' ) : void
$type string The type of caching headers to send. One of: 'public', 'private_no_expire', 'private', 'nocache', or ''.
return void

changed() public static method

Determine if the session data has changed
public static changed ( ) : boolean
return boolean True if the session data has changed

clear_userid() public static method

Clear the user_id attached to sessions, delete other sessions that are associated to the user_id
public static clear_userid ( integer $user_id )
$user_id integer The user_id to clear.

create() public static method

Create a new session - that is, generate an ID and set the session cookie
public static create ( )

destroy() public static method

Destroy stored session data by session id
public static destroy ( )

error() public static method

Store an error message in the user's session
public static error ( string $error, string $key = null )
$error string The error message
$key string An optional id that would guarantee a single unique message for this key

gc() public static method

Session garbage collection deletes expired sessions
public static gc ( )

get_error() public static method

Retrieve a specific error from stored errors.
public static get_error ( string $key, boolean $clear = true ) : string
$key string ID of the error to retrieve
$clear boolean true to clear the error from the session upon receipt
return string Return the error message

get_errors() public static method

Get all error messages from the user session
public static get_errors ( boolean $clear = true ) : array
$clear boolean true to clear the messages from the session upon receipt
return array And array of error messages

get_notice() public static method

Retrieve a specific notice from stored errors.
public static get_notice ( string $key, boolean $clear = true ) : string
$key string ID of the notice to retrieve
$clear boolean true to clear the notice from the session upon receipt
return string Return the notice message

get_notices() public static method

Get all notice messages from the user session
public static get_notices ( boolean $clear = true ) : array
$clear boolean true to clear the messages from the session upon receipt
return array And array of notice messages

get_set() public static method

Return a set of messages
public static get_set ( string $set, boolean $clear = true ) : array
$set string The name of the message set
$clear boolean true to clear the messages from the session upon receipt
return array An array of message strings

get_subnet() protected static method

We use this to store subnets for each IPv4 session, rather than distinct IPs, which could be pooled or rotated on large networks.
protected static get_subnet ( string $remote_address = '' ) : integer | string
$remote_address string The remote host's IP address.
return integer | string The numeric subnet, if IPv4. The complete address, as passed, if a valid IPv6.

has_errors() public static method

Determine if there are error messages to display
public static has_errors ( string $key = null ) : boolean
$key string Optional key of the unique error message
return boolean true if there are errors, false if not

has_messages() public static method

Determine if there are messages that should be displayed Messages are not cleared when calling this function.
public static has_messages ( ) : boolean
return boolean true if there are messages to display.

init() public static method

Initialize the session handlers
public static init ( )

messages_get() public static method

Return output of notice and error messages
public static messages_get ( boolean $clear = true, array $callback = null ) : mixed
$clear boolean true to clear the messages from the session upon receipt
$callback array a reference to a callback function for formatting the the messages or the string 'array' to get a raw array
return mixed output of messages

messages_out() public static method

Output notice and error messages
public static messages_out ( boolean $clear = true, null | callable $callback = null )
$clear boolean true to clear the messages from the session upon receipt
$callback null | callable A reference to a callback function for formatting the messages

notice() public static method

Store a notice message in the user's session
public static notice ( string $notice, string $key = null )
$notice string The notice message
$key string An optional id that would guarantee a single unique message for this key

process_queue() public static method

Call update() on QueryRecords or execute the function queued with self::queue()
public static process_queue ( )

queue() public static method

Add objects or functions to the post-processing queue, executed via self::process_queue()
public static queue ( QueryRecord | Callable $fn, null | string $name = null )
$fn QueryRecord | Callable A QueryRecord to call ->update() on, or a function to execute
$name null | string An index to save the queue under, defaults to the SPL object hash for objects

read() public static method

Verifies against a number of parameters for security purposes.
public static read ( ) : boolean
return boolean Whether a session was loaded or not

remove_error() public static method

Removes a specific error from the stored errors.
public static remove_error ( string $key ) : boolean
$key string ID of the error to remove
return boolean True or false depending if the error was removed successfully.

remove_notice() public static method

Removes a specific notice from the stored notices.
public static remove_notice ( string $key ) : boolean
$key string ID of the notice to remove
return boolean True or false depending if the notice was removed successfully.

session_data_hash() public static method

Get a hash of the contents of the session data for change comparison
public static session_data_hash ( ) : string
return string a hash of the session data

set_userid() public static method

Sets the user_id attached to the current session
public static set_userid ( integer $user_id )
$user_id integer The user id of the current user

shutdown() public static method

public static shutdown ( )

write() public static method

Commit $_SESSION data to the database for this user.
public static write ( )

Property Details

$session_changed static public property

static public $session_changed

$session_id static public property

static public $session_id

$stored_session_hash static public property

static public $stored_session_hash

$write_queue static public property

* The initial data. Used to determine whether we should write anything.
static public $write_queue