您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章给大家分享的是有关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) { 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";
以上就是php中怎么自定义session,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。