相思资源网 Design By www.200059.com

下面为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";

标签:
自定义session

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“php自定义session示例分享”

暂无php自定义session示例分享的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。