php自定义session示例分享
2015-01-24信息快讯网
这篇文章主要介绍了php自定义session示例,需要的朋友可以参考下
下面为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";
安装ImageMagick出现error while loading shared libraries的解决方法
php实现的CSS更新类实例
PHP实现AES256加密算法实例
php中unserialize返回false的解决方法
PHP提示Cannot modify header information - headers already sent by解决方法
php使用$_POST或$_SESSION[]向js函数传参
MyEclipse常用配置图文教程
PHP利用MySQL保存session的实现思路及示例代码
thinkphp在模型中自动完成session赋值示例代码
如何让thinkphp在模型中自动完成session赋值小教程
PHP中cookie和session的区别实例分析
Windows下的PHP安装文件线程安全和非线程安全的区别
php cli换行示例
PHP函数addslashes和mysql_real_escape_string的区别
PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等)
php setcookie函数的参数说明及其用法
从PHP的源码中深入了解stdClass类
理解PHP中的stdClass类
php sybase_fetch_array使用方法
Session的工作机制详解和安全性问题(PHP实例讲解)
PHP设计模式之观察者模式(Observer)详细介绍和代码实例
PHP中Session引起的脚本阻塞问题解决办法
PHP中使用memcache存储session的三种配置方法
php中session使用示例
php设置session值和cookies的学习示例