当前位置: 移动技术网 > IT编程>开发语言>PHP > php自定义session示例分享

php自定义session示例分享

2019年03月27日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网