代码实现,首先创建数据表,MySQL 的 SQL 语句如下:
- CREATE TABLE `sess` (
-
- `sesskey` varchar(32) NOT NULL default ”,
-
- `expiry` bigint(20) NOT NULL default ‘0′,
-
- `data` longtext NOT NULL,
-
- PRIMARY KEY (`sesskey`),
-
- KEY `expiry` (`expiry`)
-
- ) TYPE=MyISAM
sesskey 为 SESSION ID,expiry 为 SESSION 过期时间,data 用于保存 SESSION 数据,默认情况下 SESSION 数据是以文件方式保存,想要使用数据库方式保存,就必须重新定义 SESSION 各个操作的处理函数,PHP 提供了session_set_save_handle() 函数,可以用此函数自定义 SESSION 的处理过程,当然首先要先将 session.save_handler 改成 user,可在 PHP 中进行设置,代码如下:
<?php session_module_name(‘user‘); ?>
接下来着重讲一下 session_set_save_handle() 函数,此函数有六个参数:
session_set_save_handler(string open,string close,string read,string write,string destroy,string gc)
各个参数为各项操作的函数名,这些操作依次是:打开、关闭、读取、写入、销毁、垃圾回收,PHP 手册中有详细的例子,在这里我们使用 OO 的方式来实现这些操作,详细代码如下:
- <?php
- define(‘MY_SESS_TIME‘, 3600);
-
- class My_Sess
- {
- function init()
- {
- $domain = ‘.infor96.com‘;
-
- ini_set(‘session.use_trans_sid‘, 0);
-
- ini_set(‘session.gc_maxlifetime‘, MY_SESS_TIME);
-
-
- ini_set(‘session.use_cookies‘, 1);
- ini_set(‘session.cookie_path‘, ‘/‘);
-
- ini_set(‘session.cookie_domain‘, $domain);
-
-
- session_module_name(‘user‘);
-
- session_set_save_handler(
- array(‘My_Sess‘, ‘open‘),
- array(‘My_Sess‘, ‘close‘),
- array(‘My_Sess‘, ‘read‘),
- array(‘My_Sess‘, ‘write‘),
- array(‘My_Sess‘, ‘destroy‘),
- array(‘My_Sess‘, ‘gc‘)
- );
- }
- function open($save_path, $session_name) {
- return true;
- }
- function close() {
- global $MY_SESS_CONN;
-
- if ($MY_SESS_CONN) {
- $MY_SESS_CONN->Close();
- }
- return true;
- }
- function read($sesskey) {
- global $MY_SESS_CONN;
-
- $sql = ‘SELECT data FROM sess WHERE sesskey=‘ . $MY_SESS_CONN->qstr($sesskey) . ‘ AND expiry>=‘ . time();
- $rs =& $MY_SESS_CONN->Execute($sql);
- if ($rs) {
- if ($rs->EOF) {
- return ”;
- } else {
- $v = $rs->fields[0];
- $rs->Close();
- return $v;
- }
- }
- return ”;
- }
- function write($sesskey, $data) {
- global $MY_SESS_CONN;
-
- $qkey = $MY_SESS_CONN->qstr($sesskey);
- $expiry = time() + My_SESS_TIME;
-
- $arr = array(
- ‘sesskey‘ => $qkey,
- ‘expiry‘ => $expiry,
- ‘data‘ => $data);
- $MY_SESS_CONN->Replace(‘sess‘, $arr, ‘sesskey‘, $autoQuote = true);
- return true;
- }
- function destroy($sesskey) {
- global $MY_SESS_CONN;
-
- $sql = ‘DELETE FROM sess WHERE sesskey=‘ . $MY_SESS_CONN->qstr($sesskey);
- $rs =& $MY_SESS_CONN->Execute($sql);
- return true;
- }
- function gc($maxlifetime = null) {
- global $MY_SESS_CONN;
-
- $sql = ‘DELETE FROM sess WHERE expiry< ‘ . time();
- $MY_SESS_CONN->Execute($sql);
-
-
- $sql = ‘OPTIMIZE TABLE sess‘;
- $MY_SESS_CONN->Execute($sql);
- return true;
- }
- }
-
- require_once(‘adodb/adodb.inc.php‘);
-
- $db_type = ‘mysql‘;
-
- $db_host = ‘192.168.212.1‘;
- $db_user = ‘sess_user‘;
- $db_pass = ‘sess_pass‘;
- $db_name = ‘sess_db‘;
-
- $GLOBALS['MY_SESS_CONN'] =& ADONewConnection($db_type);
- $GLOBALS['MY_SESS_CONN']->Connect( $db_host, $db_user, $db_pass, $db_name);
-
- My_Sess::init();
- ?>
|