SESSION存放在数据库用法实例

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

学习php笔记 字符串处理

substr(); substr(str,start,length); 例:substr('php is very good language',4,5); 输出为 is ve; 当st...

PHPExcel导出2003和2007的excel文档功能示例

本文实例讲述了PHPExcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下: require_once 'common/excel/PHPExcel.p...

功能强大的PHP图片处理类(水印、透明度、旋转)

非常强大的php图片处理类,可以自定义图片水印、透明度、图片缩放、图片锐化、图片旋转、图片翻转、图片剪切、图片反色。  * 图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转...

windows平台中配置nginx+php环境

刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是"引擎x",一般引"擎代"表了性能,而"x"大多出现是表示"xtras(额外的效果)",那么整个词的意思就是类似"...

浅谈php7的重大新特性

浅谈php7的重大新特性

截止到目前为止,PHP官方已经发布了php7的RC5版本,预计在11月份左右会发布第一个正式版本!现在来说php7的重大特性肯定已经是定型了,不会再有什么变动了。后续一些版本的迭代主要也...