ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] * * * * * ## 1 session操作源代码(thinkphp\library\think\Session.php) ~~~ protected static $prefix = ''; protected static $active = false; ~~~ ~~~ public static function prefix($prefix = '') { if (empty($prefix) && null !== $prefix) { return self::$prefix; } else { self::$prefix = $prefix; } } ~~~ ~~~ public static function init(array $config = []) { if (empty($config)) { $config = Config::get('session'); } APP_DEBUG && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info'); $isDoStart = false; if (isset($config['use_trans_sid'])) { ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); } if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) { ini_set('session.auto_start', 0); $isDoStart = true; } if (isset($config['prefix'])) { self::$prefix = $config['prefix']; } if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) { session_id($_REQUEST[$config['var_session_id']]); } elseif (isset($config['id']) && !empty($config['id'])) { session_id($config['id']); } if (isset($config['name'])) { session_name($config['name']); } if (isset($config['path'])) { session_save_path($config['path']); } if (isset($config['domain'])) { ini_set('session.cookie_domain', $config['domain']); } if (isset($config['expire'])) { ini_set('session.gc_maxlifetime', $config['expire']); ini_set('session.cookie_lifetime', $config['expire']); } if (isset($config['use_cookies'])) { ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0); } if (isset($config['cache_limiter'])) { session_cache_limiter($config['cache_limiter']); } if (isset($config['cache_expire'])) { session_cache_expire($config['cache_expire']); } if (!empty($config['type'])) { $class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\session\\driver\\') . ucwords($config['type']); if (!class_exists($class) || !session_set_save_handler(new $class($config))) { throw new \think\Exception('error session handler', 11700); } } if ($isDoStart) { session_start(); self::$active = true; } } ~~~ ~~~ public static function set($name, $value = '', $prefix = null) { if (!self::$active) { self::init(); } $prefix = !is_null($prefix) ? $prefix : self::$prefix; if (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); if ($prefix) { $_SESSION[$prefix][$name1][$name2] = $value; } else { $_SESSION[$name1][$name2] = $value; } } elseif ($prefix) { $_SESSION[$prefix][$name] = $value; } else { $_SESSION[$name] = $value; } } ~~~ ~~~ public static function get($name = '', $prefix = null) { if (!self::$active) { self::init(); } $prefix = !is_null($prefix) ? $prefix : self::$prefix; if ('' == $name) { $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION; } elseif ($prefix) { if (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); $value = isset($_SESSION[$prefix][$name1][$name2]) ? $_SESSION[$prefix][$name1][$name2] : null; } else { $value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null; } } else { if (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); $value = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null; } else { $value = isset($_SESSION[$name]) ? $_SESSION[$name] : null; } } return $value; } ~~~ ~~~ public static function delete($name, $prefix = null) { $prefix = !is_null($prefix) ? $prefix : self::$prefix; if (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); if ($prefix) { unset($_SESSION[$prefix][$name1][$name2]); } else { unset($_SESSION[$name1][$name2]); } } else { if ($prefix) { unset($_SESSION[$prefix][$name]); } else { unset($_SESSION[$name]); } } } ~~~ ~~~ public static function clear($prefix = null) { $prefix = !is_null($prefix) ? $prefix : self::$prefix; if ($prefix) { unset($_SESSION[$prefix]); } else { $_SESSION = []; } } ~~~ ~~~ public static function has($name, $prefix = null) { if (!self::$active) { self::init(); } $prefix = !is_null($prefix) ? $prefix : self::$prefix; if (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]); } else { return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]); } } ~~~ ~~~ public static function pause() { session_write_close(); } ~~~ ~~~ public static function start() { session_start(); self::$active = true; } ~~~ ~~~ public static function destroy() { $_SESSION = []; session_unset(); session_destroy(); } ~~~ ~~~ private static function regenerate() { session_regenerate_id(); } ~~~ ## 2 文件分析 1 静态变量 > $prifex:session前缀 > $active:???? 2 `public static function prefix($prefix = ''){}` 设置session前缀 > $prefix:session前缀 3 `public static function init(array $config = []){}` 初始化session > $config:session配置参数 4 `public static function set($name, $value = '', $prefix = null){}` 设置session值 > $name:session名称 > $value:session值 > $prefix:session前缀 5 `public static function get($name = '', $prefix = null){}` 获取session值 > $name:session名称 > $prefix:session前缀 6`public static function delete($name, $prefix = null){}` 删除session数据 > $name:session名称 > $prefix:session前缀 7 `public static function clear($prefix = null){}` 清空session数据 > $prefix:session前缀 8 `public static function has($name, $prefix = null){}` 判断session数据 > $name:session名称 > $prefix:session前缀 9 ~~~ public static function pause(){} public static function start(){} public static function destroy(){} ~~~ 暂停 启动 销毁session 10 `private static function regenerate()` 重新生成session_id ## 3 使用方法 TODO ## 4 总结 TODO