<?php /** * ----------------------------------- * session * ----------------------------------- * Author: 安徽宝讯信息技术有限公司 * Creater: Hoogl * Date: 2020/11/3 * Version: 1.0.0 * ----------------------------------- */ function sess_open($save_path, $session_name) { return true; } function sess_close() { return true; } function sess_read($key) { global $dsql; if (empty($dsql)) { require_once(DEDEDATA.'/common.inc.php'); if ( $GLOBALS['cfg_dbtype'] =='mysql' ) { if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect')) { require_once(DEDEINC.'/dedesqli.class.php'); $dsql = new DedeSqli(FALSE); } else { require_once(DEDEINC.'/dedesql.class.php'); $dsql = new DedeSql(FALSE); } } else { require_once(DEDEINC.'/dedesqlite.class.php'); $dsql = new DedeSqlite(FALSE); } } $res = $dsql->GetOne("Select value,expiry_time From `#@__session` where `key`='{$key}'"); if ($res && $res['expiry_time'] >= time()) { return strval($res['value']); } return ''; } function sess_write($key, $val) { if (empty($val)) { return true; } global $dsql; if (empty($dsql)) { require_once(DEDEDATA.'/common.inc.php'); if ( $GLOBALS['cfg_dbtype'] =='mysql' ) { if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect')) { require_once(DEDEINC.'/dedesqli.class.php'); $dsql = new DedeSqli(FALSE); } else { require_once(DEDEINC.'/dedesql.class.php'); $dsql = new DedeSql(FALSE); } } else { require_once(DEDEINC.'/dedesqlite.class.php'); $dsql = new DedeSqlite(FALSE); } } $max_life_time = intval(get_cfg_var("session.gc_maxlifetime")); $max_life_time = $max_life_time ?: 1800; $expiry_time = time() + $max_life_time; //过期时间 $value = addslashes($val); $data = unSerializeStr($val); $member_id = empty($data['member_id']) ? 0 : intval($data['member_id']); $res = $dsql->GetOne("Select `value` From `#@__session` where `key`='{$key}'"); if (empty($res)) { $dbRes = $dsql->ExecuteNoneQuery("INSERT INTO `#@__session` (`key`,`value`,`member_id`,`expiry_time`) VALUES ('$key','$value',{$member_id},{$expiry_time}); "); } else { $dbRes = $dsql->ExecuteNoneQuery("update `#@__session` set value='" . $value . "',member_id=".$member_id.",expiry_time=" . $expiry_time . " where `key`='" . $key . "';"); } return $dbRes; } function sess_destroy($key) { global $dsql; if (empty($dsql)) { require_once(DEDEDATA.'/common.inc.php'); if ( $GLOBALS['cfg_dbtype'] =='mysql' ) { if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect')) { require_once(DEDEINC.'/dedesqli.class.php'); $dsql = new DedeSqli(FALSE); } else { require_once(DEDEINC.'/dedesql.class.php'); $dsql = new DedeSql(FALSE); } } else { require_once(DEDEINC.'/dedesqlite.class.php'); $dsql = new DedeSqlite(FALSE); } } $dsql->ExecuteNoneQuery("delete from `#@__session` where `key`='" . $key . "';"); return true; } function sess_gc($maxlifetime) { global $dsql; if (empty($dsql)) { require_once(DEDEDATA.'/common.inc.php'); if ( $GLOBALS['cfg_dbtype'] =='mysql' ) { if ($GLOBALS['cfg_mysql_type'] == 'mysqli' && function_exists("mysqli_init") || !function_exists('mysql_connect')) { require_once(DEDEINC.'/dedesqli.class.php'); $dsql = new DedeSqli(FALSE); } else { require_once(DEDEINC.'/dedesql.class.php'); $dsql = new DedeSql(FALSE); } } else { require_once(DEDEINC.'/dedesqlite.class.php'); $dsql = new DedeSqlite(FALSE); } } $dsql->ExecuteNoneQuery("delete from `#@__session` where `expiry_time` < " . time() . ";"); return true; } /** * 解析session数据 * * @param $sessionData * @return array */ function unSerializeStr($sessionData) { $return_data = []; $offset = 0; while ($offset < strlen($sessionData)) { $pos = strpos($sessionData, "|", $offset); if (!strstr(substr($sessionData, $offset), "|")) { return []; } $num = $pos - $offset; $varName = substr($sessionData, $offset, $num); $offset += $num + 1; $data = unserialize(substr($sessionData, $offset)); $return_data[$varName] = $data; $offset += strlen(serialize($data)); } return $return_data; } session_set_save_handler( "sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc"); if (!session_id()) { session_start(); }