fSessionstatic classv1.0.0b10

Wraps the session control functions and the $_SESSION superglobal for a more consistent and safer API

A Cannot send session cache limiter warning will be triggered if open(), add(), clear(), delete(), get() or set() is called after output has been sent to the browser. To prevent such a warning, explicitly call open() before generating any output.

Changes:
1.0.0b10Fixed some documentation bugs 3/3/10
1.0.0b9Fixed a bug in destroy() where sessions weren't always being properly destroyed 12/8/09
1.0.0b8Fixed a bug that made the unit tests fail on PHP 5.1 10/27/09
1.0.0b7Backwards Compatibility Break - Removed the $prefix parameter from the methods delete(), get() and set() - added the methods add(), enablePersistence(), regenerateID() 10/23/09
1.0.0b6Backwards Compatibility Break - the first parameter of clear() was removed, use delete() instead 5/8/09
1.0.0b5Added documentation about session cache limiter warnings 5/4/09
1.0.0b4The class now works with existing sessions 5/4/09
1.0.0b3Fixed clear() to properly handle when $key is NULL 2/5/09
1.0.0b2Made open() public, fixed some consistency issues with setting session options through the class 1/6/09
1.0.0bThe initial implementation 6/14/07

Static Methods

::add() public

Adds a value to an already-existing array value, or to a new array value

Signature

void add( string $key, mixed $value )

Parameters

string $key The name to access the array under
mixed $value The value to add to the array

::clear() public

Removes all session values with the provided prefix

This method will not remove session variables used by this class, which are prefixed with <a href=\"fSession\">fSession</a>::.

Signature

void clear( string $prefix=NULL )

Parameters

string $prefix The prefix to clear all session values for

::close() public

Closes the session for writing, allowing other pages to open the session

Signature

void close( )

::delete() public

Deletes a value from the session

Signature

void delete( string $key )

Parameters

string $key The key of the value to delete

::destroy() public

Destroys the session, removing all values

Signature

void destroy( )

::enablePersistence() public

Changed the session to use a time-based cookie instead of a session-based cookie

The length of the time-based cookie is controlled by setLength(). When this method is called, a time-based cookie is used to store the session ID. This means the session can persist browser restarts. Normally, a session-based cookie is used, which is wiped when a browser restart occurs.

This method should be called during the login process and will normally be controlled by a checkbox or similar where the user can indicate if they want to stay logged in for an extended period of time.

Signature

void enablePersistence( )

::get() public

Gets data from the $_SESSION superglobal

Signature

mixed get( string $key, mixed $default_value=NULL )

Parameters

string $key The name to get the value for
mixed $default_value The default value to use if the requested key is not set

Returns

The data element requested

::ignoreSubdomain() public

Sets the session to run on the main domain, not just the specific subdomain currently being accessed

This method should be called after any calls to session_set_cookie_params().

Signature

void ignoreSubdomain( )

::open() public

Opens the session for writing, is automatically called by clear(), get() and set()

A Cannot send session cache limiter warning will be triggered if this, add(), clear(), delete(), get() or set() is called after output has been sent to the browser. To prevent such a warning, explicitly call this method before generating any output.

Signature

void open( boolean $cookie_only_session_id=TRUE )

Parameters

boolean $cookie_only_session_id If the session id should only be allowed via cookie - this is a security issue and should only be set to FALSE when absolutely necessary

::regenerateID() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Regenerates the session ID, but only once per script execution

Signature

void regenerateID( )

::reset() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Resets the configuration of the class

Signature

void reset( )

::set() public

Sets data to the $_SESSION superglobal

Signature

void set( string $key, mixed $value )

Parameters

string $key The name to save the value under
mixed $value The value to store

::setLength() public

Sets the minimum length of a session - PHP might not clean up the session data right away once this timespan has elapsed

Please be sure to set a custom session path via setPath() to ensure another site on the server does not garbage collect the session files from this site!

Both of the timespan can accept either a integer timespan in seconds, or an english description of a timespan (e.g. '30 minutes', '1 hour', '1 day 2 hours').

Signature

void setLength( string|integer $normal_timespan, string|integer $persistent_timespan=NULL )

Parameters

string|integer $normal_timespan The normal, session-based cookie, length for the session
string|integer $persistent_timespan The persistent, timed-based cookie, length for the session - this is enabled by calling enabledPersistence() during login

::setPath() public

Sets the path to store session files in

This method should always be called with a non-standard directory whenever setLength() is called to ensure that another site on the server does not garbage collect the session files for this site.

Standard session directories usually include /tmp and /var/tmp.

Signature

void setPath( string|fDirectory $directory )

Parameters

string|fDirectory $directory The directory to store session files in