Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php自定义session实例代码

php自定义session实例代码

发布时间:2018-05-03   编辑:www.jquerycn.cn
本文介绍了php自定义session的实现代码,有关php session的使用方法,有需要的朋友参考下。

例子,php 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)
 { // www.jbxue.com
  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
}

2,调用方法(php自定义session):
 

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

您可能感兴趣的文章:
zf框架session会话周期与次数限制
php session()函数使用方法详解
session 教程二
php自定义session实例代码
php自定文件保存session实现方法
php中Session使用方法详解(非常全面)
php中session与thinkphp中session的一些用法
session 的工作原理与session用法
php中Session工作原理与用法详解
PHP Session使用方法Session 应用实例

关键词: php  session   
[关闭]