123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- namespace think;
- use think\exception\ClassNotFoundException;
- class Session
- {
- protected static $prefix = '';
- protected static $init = null;
-
- public static function prefix($prefix = '')
- {
- empty(self::$init) && self::boot();
- 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 || null === self::$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['secure'])) {
- ini_set('session.cookie_secure', $config['secure']);
- }
- if (isset($config['httponly'])) {
- ini_set('session.cookie_httponly', $config['httponly']);
- }
- 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 = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
-
- if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
- throw new ClassNotFoundException('error session handler:' . $class, $class);
- }
- }
- if ($isDoStart) {
- session_start();
- self::$init = true;
- } else {
- self::$init = false;
- }
- }
-
- public static function boot()
- {
- if (is_null(self::$init)) {
- self::init();
- } elseif (false === self::$init) {
- if (PHP_SESSION_ACTIVE != session_status()) {
- session_start();
- }
- self::$init = true;
- }
- }
-
- public static function set($name, $value = '', $prefix = null)
- {
- empty(self::$init) && self::boot();
- $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)
- {
- empty(self::$init) && self::boot();
- $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 pull($name, $prefix = null)
- {
- $result = self::get($name, $prefix);
- if ($result) {
- self::delete($name, $prefix);
- return $result;
- } else {
- return;
- }
- }
-
- public static function flash($name, $value)
- {
- self::set($name, $value);
- if (!self::has('__flash__.__time__')) {
- self::set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']);
- }
- self::push('__flash__', $name);
- }
-
- public static function flush()
- {
- if (self::$init) {
- $item = self::get('__flash__');
- if (!empty($item)) {
- $time = $item['__time__'];
- if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) {
- unset($item['__time__']);
- self::delete($item);
- self::set('__flash__', []);
- }
- }
- }
- }
-
- public static function delete($name, $prefix = null)
- {
- empty(self::$init) && self::boot();
- $prefix = !is_null($prefix) ? $prefix : self::$prefix;
- if (is_array($name)) {
- foreach ($name as $key) {
- self::delete($key, $prefix);
- }
- } elseif (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)
- {
- empty(self::$init) && self::boot();
- $prefix = !is_null($prefix) ? $prefix : self::$prefix;
- if ($prefix) {
- unset($_SESSION[$prefix]);
- } else {
- $_SESSION = [];
- }
- }
-
- public static function has($name, $prefix = null)
- {
- empty(self::$init) && self::boot();
- $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 push($key, $value)
- {
- $array = self::get($key);
- if (is_null($array)) {
- $array = [];
- }
- $array[] = $value;
- self::set($key, $array);
- }
-
- public static function start()
- {
- session_start();
- self::$init = true;
- }
-
- public static function destroy()
- {
- if (!empty($_SESSION)) {
- $_SESSION = [];
- }
- session_unset();
- session_destroy();
- self::$init = null;
- }
-
- public static function regenerate($delete = false)
- {
- session_regenerate_id($delete);
- }
-
- public static function pause()
- {
-
- session_write_close();
- self::$init = false;
- }
- }
|