کد:
class dbSession
{
/**
* Constructor of class
*
* Initializes the class and starts a new session
*
* There is no need to call start_session() after instantiating this class
*
* @param integer $gc_maxlifetime the number of seconds after which data will be seen as 'garbage' and
* cleaned up on the next run of the gc (garbage collection) routine
*
* Default is specified in php.ini file
*
* @param integer $gc_probability used in conjunction with gc_divisor, is used to manage probability that
* the gc routine is started. the probability is expressed by the formula
*
* probability = $gc_probability / $gc_divisor
*
* So if $gc_probability is 1 and $gc_divisor is 100 means that there is
* a 1% chance the the gc routine will be called on each request
*
* Default is specified in php.ini file
*
* @param integer $gc_divisor used in conjunction with gc_probability, is used to manage probability
* that the gc routine is started. the probability is expressed by the formula
*
* probability = $gc_probability / $gc_divisor
*
* So if $gc_probability is 1 and $gc_divisor is 100 means that there is
* a 1% chance the the gc routine will be called on each request
*
* Default is specified in php.ini file
*
* @return void
*/
//this function run
function dbSession($gc_maxlifetime = "600", $gc_probability = "", $gc_divisor = "")
{
global $settings;
if($_COOKIE['rememberMe__ID_main'] == md5("3month"))
{
$gc_maxlifetime = 8035200;
}
elseif($_COOKIE['rememberMe__ID_main'] == md5("6month"))
{
$gc_maxlifetime = 16070400;
}
else
{
$gc_maxlifetime = $settings['session_lifetime'];
}
// if $gc_maxlifetime is specified and is an integer number
if ($gc_maxlifetime != "" && is_integer($gc_maxlifetime)) {
// set the new value
@ini_set('session.gc_maxlifetime', $gc_maxlifetime);
}
// if $gc_probability is specified and is an integer number
if ($gc_probability != "" && is_integer($gc_probability)) {
// set the new value
@ini_set('session.gc_probability', $gc_probability);
}
// if $gc_divisor is specified and is an integer number
if ($gc_divisor != "" && is_integer($gc_divisor)) {
// set the new value
@ini_set('session.gc_divisor', $gc_divisor);
}
// get session lifetime
$this->sessionLifetime = ini_get("session.gc_maxlifetime");
// register the new handler
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
register_shutdown_function('session_write_close');
// start the session
session_start();
}
/**
* Deletes all data related to the session
*
* @return void
*/
function stop()
{
$this->regenerate_id();
session_unset();
session_destroy();
}
/**
* Regenerates the session id.
*
* <b>Call this method whenever you do a privilege change!</b>
*
* @return void
*/
function regenerate_id()
{
// saves the old session's id
$oldSessionID = session_id();
// regenerates the id
// this function will create a new session, with a new id and containing the data from the old session
// but will not delete the old session
session_regenerate_id();
// because the session_regenerate_id() function does not delete the old session,
// we have to delete it manually
$this->destroy($oldSessionID);
}
/**
* Get the number of online users
*
* This is not 100% accurate. It depends on how often the garbage collector is run
*
* @return integer approximate number of users currently online
*/
function get_users_online()
{
global $db;
// counts the rows from the database
$query = $db->simple_select(TABLE_PREFIX."session_data", $fields="COUNT(session_id) as count", $conditions="online_expire > ".time()."", $options=array());
$result = $db->fetch_array($query);
// return the number of found rows
return $result["count"];
}
/**
* Custom open() function
*
* @access private
*/
function open($save_path, $session_name)
{
return true;
}
/**
* Custom close() function
*
* @access private
*/
function close()
{
return true;
}
/**
* Custom read() function
*
* @access private
*/
function read($session_id)
{
global $db;
// reads session data associated with the session id
// but only if the HTTP_USER_AGENT is the same as the one who had previously written to this session
// and if session has not expired
$result = $db->simple_select(TABLE_PREFIX."session_data", $fields="session_data", $conditions="session_id = '".$session_id."' AND http_user_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND session_expire > '".time()."'");
// if anything was found
if (is_resource($result) && @mysql_num_rows($result) > 0) {
// return found data
$fields = @mysql_fetch_assoc($result);
// don't bother with the unserialization - PHP handles this automatically
return $fields["session_data"];
}
// if there was an error return an empty string - this HAS to be an empty string
return "";
}
/**
* Custom write() function
*
* @access private
*/
function write($session_id, $session_data)
{
global $db,$settings;
// first checks if there is a session with this id
$result = $db->simple_select(TABLE_PREFIX."session_data", $fields="session_data", $conditions="session_id = '".$session_id."'");
// if there is
if (@mysql_num_rows($result) > 0) {
// update the existing session's data
// and set new expiry time
$array = array();
$array['session_data'] = $session_data;
$array['online_expire'] = (time() + $settings['online_expire_time_limit']);
$array['session_expire'] = (time() + $this->sessionLifetime);
$array['memberID'] = $_SESSION['memberID'];
$array['request_URI'] = $_SERVER['REQUEST_URI'];
$array['session_id'] = $session_id;
$result = $db->update_query(TABLE_PREFIX."session_data", $array, $where="session_id = '".$session_id."'");
// if anything happened
if (@mysql_affected_rows()) {
// return true
return true;
}
// if this session id is not in the database
} else {
// insert a new record
$array = array();
$array['session_id'] = $session_id;
$array['http_user_agent'] = $_SERVER["HTTP_USER_AGENT"];
$array['session_data'] = $session_data;
$array['IP'] = $_SERVER['REMOTE_ADDR'];
$array['online_expire'] = (time() + $settings['online_expire_time_limit']);
$array['request_URI'] = $_SERVER['REQUEST_URI'];
$array['session_expire'] = (time() + $this->sessionLifetime);
$result = $db->insert_query(TABLE_PREFIX."session_data", $array);
// if anything happened
if (@mysql_affected_rows()) {
// return an empty string
return "";
}
}
// if something went wrong, return false
return false;
}
/**
* Custom destroy() function
*
* @access private
*/
/* Destroy session record in database */
function destroy($session_id) {
$session_sql = "DELETE FROM ".TABLE_PREFIX."session_data WHERE session_id = '$session_id'";
$session_res = mysql_query($session_sql);
if (!$session_res) {
return false;
} else {
return true;
}
}
/**
* Custom gc() function (garbage collector)
*
* @access private
*/
function gc($maxlifetime)
{
global $db;
// it deletes expired sessions from database
$result = $db->delete_query(TABLE_PREFIX."session_data", $where="session_expire < '".(time() - $maxlifetime)."'");
}