php代码
//===========================================
// 程序: MySQL-based session Class
// 功能: 基于mysql存储的 Session 功能类
// 作者: yejr
// 网站: http://imysql.cn
// 时间: 2007-01-05
//===========================================
//设定 SESSION 有效时间,单位是 秒
define('SESS_LIFTTIME', 3600);
define('DB_DSN', 'mysql://root:@localhost/test');
define('DB_NAME','test');
require_once('DB.php');
$gDb = new DB;
$gDb = DB::connect(DB_DSN, TRUE);
if (PEAR::isError($gDb))
{
die($gDb->getMessage());
}
if (!defined('MySQLSession'))
{
define('MySQLSession', TRUE);
class Usess
{
static $mSessSavePath;
static $mSessName;
static $mSessMaxTime;
static $mTblSess = 'sessions';
static $mTblSessMap;
static $mDb;
// {{{ 初始化构造函数
public function __construct()
{
self::$mSessMaxTime = SESS_LIFTTIME;
self::$mTblSessMap = array(
'sid' => 'sid',
'data' => 'session',
'last' => 'flush_dt',
);
}
// }}}
public function sessOpen($pSavePath = '', $pSessName = '')
{
global $gDb;
self::$mDb = $gDb;
self::$mSessSavePath = $pSavePath;
self::$mSessName = $pSessName;
self::sessGc();
return TRUE;
}
// }}}
public function sessClose()
{
return TRUE;
}
// }}}
public function sessRead($wSid = '')
{
global $db;
$wSql = sprintf("SELECt * FROM `%s`.`%s` WHERe `%s` = '%s';",
DB_NAME,
self::$mTblSess,
self::$mTblSessMap['sid'],
$wSid
);
//这里一定要用 DB_FETCHMODE_ASSOC,否则取回的数组只能用数字做下标
if (!PEAR::isError($row = self::$mDb->getRow($wSql, null, DB_FETCHMODE_ASSOC)))
{
//session已经存在了
if (is_array($row) && 1 <= count($row))
{
return $row[self::$mTblSessMap['data']];
}
else
{
$wSql = sprintf("INSERT INTO `%s`.`%s` VALUES ('%s', '', UNIX_TIMESTAMP(NOW()));",
DB_NAME,
self::$mTblSess,
$wSid
);
if (!PEAR::isError(self::$mDb->query($wSql)))
{
return TRUE;
}
}
}
return FALSE;
}
// }}}
public function sessWrite($wSid = '', $wData = '')
{
$wData = mysql_escape_string($wData);
$wSql = sprintf("UPDATe `%s`.`%s` SET `%s` = '%s', `%s` = UNIX_TIMESTAMP(NOW()) WHERe `%s` = '%s';",
DB_NAME,
self::$mTblSess,
self::$mTblSessMap['data'],
$wData,
self::$mTblSessMap['last'],
self::$mTblSessMap['sid'],
$wSid
);
if (!PEAR::isError(self::$mDb->query($wSql)))
{
return TRUE;
}
return FALSE;
}
// }}}
public function sessDestroy($wSid = '')
{
$wSql = sprintf("DELETE FROM `%s`.`%s` WHERe `%s` = '%s';",
DB_NAME,
self::$mTblSess,
$wSid
);
if (!PEAR::isError(self::$mDb->query($wSql)))
{
return TRUE;
}
return FALSE;
}
// }}}
public function sessGc()
{
global $db;
//计算出过期时间
$last = time() - self::$mSessMaxTime;
$wSql = sprintf("DELETe FROM `%s`.`%s` WHERe `%s` < $last;", DB_NAME, self::$mTblSess, self::$mTblSessMap['last']);
if (!PEAR::isError(self::$mDb->query($wSql)))
{
return TRUE;
}
return FALSE;
}
// }}}
public function initSess()
{
$domain = '';
//不使用 GET/POST 变量方式
ini_set('session.use_trans_sid', 0);
//设置垃圾回收最大生存时间
ini_set('session.gc_maxlifetime', SESS_LIFTTIME);
//使用 cookie 保存 SESSION ID 的方式
ini_set('session.use_cookies', 1);
ini_set('session.cookie_path', '/');
//多主机共享保存 SESSION ID 的 cookie
ini_set('session.cookie_domain', $domain);
//将 session.save_handler 设置为 user,而不是默认的 files
session_module_name('user');
//定义 SESSION 各项操作所对应的方法名:
session_set_save_handler(
array('Usess', 'sessOpen'), //对应于静态方法 My_Sess::open(),下同。
array('Usess', 'sessClose'),
array('Usess', 'sessRead'),
array('Usess', 'sessWrite'),
array('Usess', 'sessDestroy'),
array('Usess', 'sessGc')
);
session_start();
return TRUE;
}
// }}}
}//end class
}//end define
$sess = new Usess;
$sess->initSess();
?>