您好,欢迎访问一九零五行业门户网

多服务器共享session(mysql)

php代码
getmessage());  
}
if (!defined('mysqlsession'))  
{  
   define('mysqlsession',   true);
class usess  
{  
   static  $msesssavepath;  
   static  $msessname;  
   static  $msessmaxtime;  
   static  $mtblsess  = 'sessions';  
   static  $mtblsessmap;  
   static  $mdb;
// {{{ 初始化构造函数  
   /** 
* 构造函数 
* 
* @param string $login_user   登录用户 
* @param int $login_type  用户类型 
* @param string $login_sess   登录session值 
* @return esession 
*/ 
   public function __construct()  
   {  
   self::$msessmaxtime = sess_lifttime;
self::$mtblsessmap  = array(  
   'sid'  => 'sid',  
   'data'  => 'session',  
   'last'  => 'flush_dt',  
   );  
   }  
   // }}}
/** {{{ sessopen($psavepath, $name) 
* 
* @param  string  $psavepath 
* @param  string  $psessname 
* 
* @return  bool   true/false 
*/ 
   public function sessopen($psavepath = '', $psessname = '')  
   {  
   global $gdb;
self::$mdb  = $gdb;  
   self::$msesssavepath   = $psavepath;  
   self::$msessname   = $psessname;
self::sessgc();
return true;  
   }  
   // }}}
/** {{{ sessclose() 
* 
* @param  null 
* 
* @return  bool   true/false 
*/ 
   public function sessclose()  
   {  
   return true;  
   }  
   // }}}
/** {{{ sessread($wsid) 
* 
* @param  string  $wsid 
* 
* @return  bool   true/false 
*/ 
   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    {  
   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;  
   }  
   // }}}
/** {{{ sesswrite($wsid, $wdata) 
* 
* @param  string  $wsid 
* @param  string  $wdata 
* 
* @return  bool   true/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;  
   }  
   // }}}
/** {{{ sessdestroy($wsid) 
* 
* @param  string  $wsid 
* 
* @return  bool   true/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;  
   }  
   // }}}
/** {{{ sessgc() 
* 
* @param  null 
* 
* @return  bool   true/false 
*/ 
   public function sessgc()  
   {  
   global $db;
//计算出过期时间  
   $last  = time() - self::$msessmaxtime;
$wsql   = sprintf(delete from `%s`.`%s` where `%s`  
   if (!pear::iserror(self::$mdb->query($wsql)))  
   {  
   return true;  
   }
return false;  
   }  
   // }}}
/** {{{ initsess() 
* 
* @param  null 
* 
* @return  bool   true/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();  
?>
其它类似信息

推荐信息