<?php
class
MysqlSession {
private
$DB_SERVER
=
"localhost"
;
private
$DB_NAME
=
""
;
private
$DB_USER
=
"root"
;
private
$DB_PASS
=
""
;
private
$DB_SELECT_DB
=
""
;
private
$SESS_LIFE
= 0;
function
MysqlSession (&
$sessionDB
) {
$this
->DB_NAME = &
$sessionDB
;
$this
->SESS_LIFE = get_cfg_var(
"session.gc_maxlifetime"
);
session_module_name('user');
session_set_save_handler(
array
(&
$this
, 'sess_open'),
array
(&
$this
, 'sess_close'),
array
(&
$this
, 'sess_read'),
array
(&
$this
, 'sess_write'),
array
(&
$this
, 'sess_destroy'),
array
(&
$this
, 'sess_gc')
);
session_start();
}
function
sess_open(
$save_path
,
$session_name
){
if
(!
$this
->DB_SELECT_DB = mysql_pconnect(
$this
->DB_SERVER,
$this
->DB_USER,
$this
->DB_PASS)) {
echo
"SORRY! MYSQL ERROR : Can't connect to $this->DB_SERVER as $DB_USER"
;
echo
"MySQL Error: "
, mysql_error();
die
;
}
if
(! mysql_select_db(
$this
->DB_NAME,
$this
->DB_SELECT_DB)) {
echo
"SORRY! MYSQL ERROR : Unable to select database $this->DB_NAME"
;
die
;
}
return
true;
}
function
sess_close() {
return
true;
}
function
sess_read(
$SessionKey
){
$Query
=
"SELECT SessionArray FROM sessions WHERE SessionKey = '"
.
$SessionKey
.
"' AND SessionExpTime > "
. time();
$Result
= mysql_query(
$Query
,
$this
->DB_SELECT_DB);
if
(list(
$SessionArray
) = mysql_fetch_row(
$Result
)) {
return
$SessionArray
;
}
return
false;
}
function
sess_write(
$SessionKey
,
$VArray
) {
$SessionExpTime
= time() +
$this
->SESS_LIFE;
$SessionArray
=
addslashes
(
$VArray
);
$Query
=
"INSERT INTO sessions (SessionKey,SessionExpTime,SessionArray) VALUES ('"
.
$SessionKey
.
"','"
.
$SessionExpTime
.
"','"
.
$SessionArray
.
"')"
;
$Result
= mysql_query(
$Query
,
$this
->DB_SELECT_DB);
if
(!
$Result
){
$Query
=
"UPDATE sessions SET SessionExpTime = '"
.
$SessionExpTime
.
"', SessionArray = '"
.
$SessionArray
.
"' WHERE SessionKey = '"
.
$SessionKey
.
"' AND SessionExpTime > "
. time();
$Result
= mysql_query(
$Query
,
$this
->DB_SELECT_DB);
}
return
$Result
;
}
function
sess_destroy(
$SessionKey
) {
$Query
=
"DELETE FROM sessions WHERE SessionKey = '"
.
$SessionKey
.
"'"
;
$Result
= mysql_query(
$Query
,
$this
->DB_SELECT_DB);
return
$Result
;
}
function
sess_gc(
$maxlifetime
) {
$Query
=
"DELETE FROM sessions WHERE SessionExpTime < "
. time();
$Result
= mysql_query(
$Query
,
$this
->DB_SELECT_DB);
return
mysql_affected_rows(
$this
->DB_SELECT_DB);
}
}
?>