网络编程 
首页 > 网络编程 > 浏览文章

php自定义session示例分享

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

下面为session类的代码
复制代码 代码如下:
<?php
class session
{
 static function init()
 {
  session_set_save_handler(
    array("session","open"),
    array("session","close"),
    array("session","read"),
    array("session","write"),
    array("session","destroy"),
    array("session","gc")
  );
 }

 static function open($save_path,$session_name)
 {
  echo "session opening!<br>";
  /*global $db,$REMOTE_ADDR;
   $rs = $db->Execute("select * from Sessions where SessionID='".session_id()."'");
  $arry=$rs->FetchRow();
  if( $rs && $arry)
  {
  $db->Execute("update Sessions set SessionLast=NOW() where SessionID='".session_id()."'");
  }
  else
  {
  $query = "insert into Sessions set SessionID='".session_id()."',SessionName='$REMOTE_ADDR',SessionLast='NOW()'";
  //echo $query;
  $db->Execute($query);
  }*/
  return true;
 }
 static function close()
 {
  return(true);
 }

 static function read($id)
 {
  echo "session reading now!<br>";
  global $db;
  return true;
  $timenow = strftime("%Y-%m-%d %H:%M:%S", time());
  $query = "select SessionData from Sessions where SessionID='$id' and SessionLast > '$timenow'";
  $rs = $db->Execute($query);
  if(list($SessionData) = $rs->FetchRow())
  {
   //echo $SessionData;
   return $SessionData;
  }
  else
  {
   return false;
  }
 }

 static function write($id,$sess_data)
 {
  echo "session writing now!<br>";
  global $db;
  $rs = $db->Execute("select SessionID from Sessions where SessionID='$id'");
  $num = $rs->RecordCount();
  $unix_time = time()+MY_SESS_TIME;
  //echo MY_SESS_TIME;
  $dateleft = strftime("%Y-%m-%d %H:%M:%S", $unix_time);
  if($num <= 0)
  {
   $sql = "insert into Sessions set SessionData='$sess_data', SessionName='".$_SERVER["REMOTE_ADDR"]."', SessionLast='$dateleft', SessionID='".session_id()."'";
  }
  else
  {
   $sql = "update Sessions set SessionData='$sess_data', SessionName='".$_SERVER["REMOTE_ADDR"]."', SessionLast='$dateleft' where SessionID='$id'";
  }
  $db->Execute($sql);
 }

 static function destroy($id)
 {
  echo "session destroying now!<br>";
  global $db;
  $sql = "DELETE FROM Sessions WHERE `SessionID` = '$id'";
  $rs = $db->Execute($sql);
  return $rs;
  // $sess_file = "$sess_save_path/sess_$id";
  //return(@unlink($sess_file));
 }

 /*********************************************
  * WARNING - You will need to implement some *
 * sort of garbage collection routine here. *
 *********************************************/
 static function gc($maxlifetime)
 {
  echo "session maxlifetime now!<br>";
  global $db;
  $timenow = strftime("%Y-%m-%d %H:%M:%S", time());
  $sql = "DELETE FROM `$table_sessions` WHERE `SessionLast` < '$timenow'";
  return $sess_db->Execute($sql);
  //echo "now gc!<br>";
  return true;
 }
 // proceed to use sessions normally
}

使用方法
复制代码 代码如下:
include("session.class.php");
session::init();
session_start();
define("MY_SESS_TIME", 3600); //SESSION 生存时长
$_SESSION["test"] = "abcdef";

上一篇:Windows下的PHP安装文件线程安全和非线程安全的区别
下一篇:php cli换行示例
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?