欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SESSION存放在数据库用法实例

程序员文章站 2023-11-23 19:37:46
本文实例讲述了session存放在数据库用法。分享给大家供大家参考。具体如下:

本文实例讲述了session存放在数据库用法。分享给大家供大家参考。具体如下:

<?php
/*
create table `ws_sessions` (
 `session_id` varchar(255) binary not null default '',
 `session_expires` int(10) unsigned not null default '0',
 `session_data` text,
 primary key (`session_id`)
) type=innodb;
*/
class session {
 // session-lifetime
 var $lifetime;
 // mysql-handle
 var $dbhandle;
 function open($savepath, $sessname) {
 // get session-lifetime
 $this->lifetime = get_cfg_var("session.gc_maxlifetime");
 // open database-connection
 $dbhandle = @mysql_connect("localhost","root","");
 $dbsel = @mysql_select_db("test",$dbhandle);
 // return success
 if(!$dbhandle || !$dbsel)
  return false;
 $this->dbhandle = $dbhandle;
 return true;
 }
 function close() {
 $this->gc(ini_get('session.gc_maxlifetime'));
 // close database-connection
 return @mysql_close($this->dbhandle);
 }
 function read($sessid) {
 // fetch session-data
 $res = mysql_query("select session_data as d from ws_sessions
    where session_id = '$sessid'
    and session_expires > ".time(),$this->dbhandle);
 // return data or an empty string at failure
 if($row = mysql_fetch_assoc($res))
  return $row['d'];
 return "";
 }
 function write($sessid,$sessdata) {
 // new session-expire-time
 $newexp = time() + $this->lifetime;
 // is a session with this id in the database?
 $res = mysql_query("select * from ws_sessions
    where session_id = '$sessid'",$this->dbhandle);
 // if yes,
 if(mysql_num_rows($res)) {
  // ...update session-data
  mysql_query("update ws_sessions
    set session_expires = '$newexp',
    session_data = '$sessdata'
    where session_id = '$sessid'",$this->dbhandle);
  // if something happened, return true
  if(mysql_affected_rows($this->dbhandle))
  return true;
 }
 // if no session-data was found,
 else {
  // create a new row
  mysql_query("insert into ws_sessions (
    session_id,
    session_expires,
    session_data)
    values(
    '$sessid',
    '$newexp',
    '$sessdata')",$this->dbhandle);
  // if row was created, return true
  if(mysql_affected_rows($this->dbhandle))
  return true;
 }
 // an unknown error occured
 return false;
 }
 function destroy($sessid) {
 // delete session-data
 mysql_query("delete from ws_sessions where session_id = '$sessid'",$this->dbhandle);
 // if session was deleted, return true,
 if(mysql_affected_rows($this->dbhandle))
  return true;
 // ...else return false
 return false;
 }
 function gc($sessmaxlifetime) {
 // delete old sessions
 mysql_query("delete from ws_sessions where session_expires < ".time(),$this->dbhandle);
 // return affected rows
 return mysql_affected_rows($this->dbhandle);
 }
}
$session = new session();
session_set_save_handler(array(&$session,"open"),
    array(&$session,"close"),
    array(&$session,"read"),
    array(&$session,"write"),
    array(&$session,"destroy"),
    array(&$session,"gc"));
session_start();
// etc...
?>

希望本文所述对大家的php程序设计有所帮助。