php自定义session示例分享
程序员文章站
2022-10-14 20:14:49
下面为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类的代码
复制代码 代码如下:
<?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";
上一篇: PHP5中实现多态的两种方法实例分享
下一篇: 黑眼圈的形成原因 到底是什么在作祟