时间:2021-05-26
下面为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";
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了php实现session自定义会话处理器的方法。分享给大家供大家参考。具体分析如下:session自定义会话处理器,即就是说,所有关于sessio
本文实例讲述了php自定文件保存session的方法。分享给大家供大家参考。具体实现方法如下:session.inc.php文件:定义session的文件存储,
本文实例讲述了PHP基于自定义类随机生成姓名的方法。分享给大家供大家参考,具体如下:自定义类rndChinaName.class.php:getXingList
本文实例讲述了JS实现的自定义右键菜单。分享给大家供大家参考。具体如下:示例1:运行效果截图:具体代码如下:JS实现自定义右键菜单#container{text
本文介绍了dedecms织梦自定义表单导出到excel的方法示例,分享给大家,具体如下:效果展示:修改2个文件,让织梦自定义表单自由导出到Excel表格里。修改