Public Member Functions | Data Fields | Protected Member Functions
Session Class Reference

User session manager. More...

Inheritance diagram for Session:
Dispersion

Public Member Functions

 Session ()
 
 changeTable ($table)
 
 valueAt ($key)
 
 setvalueAt ($key, $value)
 
 generateRnd ($size=22)
 
 generatePass ($password, $salt=null)
 
 checkPass ($hash, $password)
 
 newSession ($data=null)
 
 checkSession ()
 
 endSession ()
 
 sqlSessionOpen ()
 
 sqlSessionCLose ()
 
 sqlSessionSelect ($sid)
 
 sqlSessionWrite ($sid, $value)
 
 sqlSessionDestroy ($sid)
 
 sqlSessionGarbageCollect ($lifetime)
 
- Public Member Functions inherited from Dispersion
 Dispersion ()
 
 emptyLayout ()
 
 set ($name, $value)
 
 _set ($name, $value)
 
 insertView ($name, $index=-1)
 

Data Fields

const PASSWORD_FIELD = 'password'
 
const LASTUSED_FIELD = 'last_used'
 
 $_table_restore
 
 $_table
 
 $_table_column = array()
 
 $_session_keys = array()
 
 $_use_db = false
 
 $_session_table = ''
 
 $_algo = '$2a'
 
 $_cost = '$10'
 
- Data Fields inherited from Dispersion
 $model_ob = null
 
 $debug_ob = null
 
 $_variables = array()
 
 $_content = array()
 
 $_content_count = 0
 
 $model
 
 $debug
 

Protected Member Functions

 restoreAndReturn ($return_value)
 
- Protected Member Functions inherited from Dispersion
 requireConfigFile ($file_name, $required=false)
 

Detailed Description

User session manager.

Version
1.2
Author
DinuSV

Member Function Documentation

Session ( )

Constructor

restoreAndReturn (   $return_value)
protected

Used to restore the table in the model after the class has finished working with it

Parameters
mixed$return_value: the value to return after the table has been restored
Returns
mixed
changeTable (   $table)

Sets table for users

Parameters
string$table
valueAt (   $key)

Get value from the session

Parameters
string$key: index from which to return the value
Returns
mixed
Exceptions
IndexOutOfBoundsException
setvalueAt (   $key,
  $value 
)

Set the value at the key specified

Parameters
string-numeric$key: index to set the value to
mixed$value: value to set
generateRnd (   $size = 22)

Generate a random string at the size specified

Parameters
numeric$size: string length
Returns
string
generatePass (   $password,
  $salt = null 
)

Generate a crypted password using the blowfish algorithm

Parameters
string$password: given string to be crypted
string$salt: salt to crypt by, auto-generated if param is missing
Returns
string : crypted value
checkPass (   $hash,
  $password 
)

Compare the password with the hash ( this can be extended for using other crypting algorithms )

Parameters
string$hash: the hash value to compare with
string$password
Returns
boolean : true if passwords match
newSession (   $data = null)

Creates a new session

Parameters
array$data: needs to be present in order to start the new session, containing keys and values to match with current ones
Returns
boolean : true, if data is valid and the session is created, false otherwise
checkSession ( )

Checks the session according to the given session keys

Returns
boolean : true if the session exists and is valid, false otherwise
endSession ( )

Ends the session

sqlSessionOpen ( )

Opens the connection and check if the table is set for the session handler

sqlSessionCLose ( )

Function that needs to be defined, in our case it does nothing

Returns
int
sqlSessionSelect (   $sid)

Reads the session data from the database

Parameters
integer$sid: the id of the session
Returns
string : the value of the session in case it's found, or empty string otherwise
sqlSessionWrite (   $sid,
  $value 
)

Writes the session data to the database

Parameters
string$sid: the session id
string$value: the value to write to the database
sqlSessionDestroy (   $sid)

Deletes all session information having the session id $sid

Parameters
string$sid
sqlSessionGarbageCollect (   $lifetime)

Deletes all sessions that have expired

Parameters
integer$lifetime: the lifetime of a session in seconds

Field Documentation

const PASSWORD_FIELD = 'password'

string : Predefined password field

const LASTUSED_FIELD = 'last_used'

string : Predefined last_used field

$_table_restore

string : Table to restore in the model after the class finishes working with the model.

$_table

string : Table used for holding user data. Example : table = 'users'

$_table_column = array()

array : Map table columns with predefined fields. There are 2 predefined fields, 'password' and 'last_used'. The 'password' field is used to alert the session class to encrypt data before matching it with the table field, and the 'last_used' stores the last time the user has logged in.

Example : $_table_column = array( 'password' => 'mypassword', 'last_used' => 'last_login' )

$_session_keys = array()

array : Map table data with the $_SESSION variables. Each key represents the name of session key to hold the data retrieved from the table, and the value represents the table column to get the data from.

Example : The following will make the password and the id of the user available in the $_SESSION variables ( $_SESSION['pwd'] = 'some password', $_SESSION['id'] = '1234' ) $_session_keys = array( 'pwd' => 'password', 'id' => 'id' )

$_use_db = false

bool : Set this to true if you want to enable sessions to be stored in a database instead of a file in the system( linux stores them in the 'tmp' directory by default ). If you set this to true, you also need to set the table in which the sessions will be stored.

$_session_table = ''

string : Table to store sessions in. Before using this, you need to enable session storage in a database, by using the field ( $_use_db ) above. The table must have the following columns and respect their properties : sid : CHAR(32), PRIMARY expiration : INT NOT NULL value : TEXT NOT NULL

$_algo = '$2a'

string : Algorithm to be used. By default, blowfish is selected with : $2a

$_cost = '$10'

string : Number of times the encryption algorithm is used. Default : $10