其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
轻松实现session的mysql处理

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

      通常,session都是保存在临时文件里的,但是,要是把它保存在数据库里,就会给我们带来很多好处,比如统计在线人数之类的。废话不说了,看程序:

<?php
include("db_mysql.inc");

function open ($save_path, $session_name) {
  global $db,$REMOTE_ADDR;
  $db->query("delete from Sessions where SessionLast<date_sub(now(),interval
1 hour)");
  if($db->query("select * from Sessions where SessionID='".session_id()."'")
&& $arry=$db->fetch_array())
     $db->query("update Sessions set SessionLast=NOW() where
SessionID='".session_id()."'");
  else $db->query("insert into Sessions set
SessionID='".session_id()."',SessionName='$REMOTE_ADDR',SessionLast='NOW()'"
);
  return(true);
}

function close()

  return true;
}

function read ($id) {
  global $db;
    if(!$db->query("select SessionID from Sessions where SessionID='$id'")
|| $db->num_rows()<=0)return false;
  $SQL="select SessionData from Sessions where SessionID='$id'";
  $db->query($SQL);
  list($sess_data)=$db->fetch_row();
  return($sess_data);
}

function write ($id, $sess_data) {
  global $db;
  if(!$db->query("select SessionID from Sessions where SessionID='$id'") ||
$db->num_rows()<=0)return false;
  if($db->query("update Sessions set
SessionData='$sess_data',SessionLast=NOW() where SessionID='$id'"))
  return true;
  else return false;
}

function destroy ($id) {
  global $db;
  $db->query("delete from Sessions where SessionID='$id'");
}

function gc ($maxlifetime) {
  return true;
}

session_set_save_handler ("open", "close", "read", "write", "destroy","gc");

session_start();

?>
注:
数据表:Sessions
CREATE TABLE Sessions (
   SessionID varchar(50) NOT NULL,
   SessionName varchar(50) NOT NULL,
   SessionData blob,
   SessionLast datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
   PRIMARY KEY (SessionID)
);




相关文章

相关软件