From e8001913bc64db123b0bd25c416161e0708603af Mon Sep 17 00:00:00 2001 From: zhylninc Date: Thu, 14 Jan 2016 21:52:55 +0800 Subject: [PATCH 1/3] session test --- library/think/SessionTest.php | 150 ++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 library/think/SessionTest.php diff --git a/library/think/SessionTest.php b/library/think/SessionTest.php new file mode 100644 index 00000000..7dee500a --- /dev/null +++ b/library/think/SessionTest.php @@ -0,0 +1,150 @@ +object = new Session; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } + + /** + * @covers think\Session::prefix + * @todo Implement testPrefix(). + */ + public function testPrefix() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::init + * @todo Implement testInit(). + */ + public function testInit() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::set + * @todo Implement testSet(). + */ + public function testSet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::get + * @todo Implement testGet(). + */ + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::delete + * @todo Implement testDelete(). + */ + public function testDelete() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::clear + * @todo Implement testClear(). + */ + public function testClear() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::has + * @todo Implement testHas(). + */ + public function testHas() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::pause + * @todo Implement testPause(). + */ + public function testPause() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::start + * @todo Implement testStart(). + */ + public function testStart() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers think\Session::destroy + * @todo Implement testDestroy(). + */ + public function testDestroy() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } +} From f22fffda2847e3e7427af7176760950cf16621ef Mon Sep 17 00:00:00 2001 From: zhylninc Date: Thu, 14 Jan 2016 22:04:13 +0800 Subject: [PATCH 2/3] fix session --- library/think/Session.php | 496 +++++++++++++++++----------------- library/think/SessionTest.php | 150 ---------- 2 files changed, 252 insertions(+), 394 deletions(-) delete mode 100644 library/think/SessionTest.php diff --git a/library/think/Session.php b/library/think/Session.php index 1e832779..42f549b5 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -1,244 +1,252 @@ - -// +---------------------------------------------------------------------- - -namespace think; - -class Session -{ - protected static $prefix = ''; - - /** - * 设置或者获取session作用域(前缀) - * @param string $prefix - * @return string|void - */ - public static function prefix($prefix = '') - { - if (empty($prefix)) { - return self::$prefix; - } else { - self::$prefix = $prefix; - } - } - - /** - * session初始化 - * @param array $config - * @return void - */ - public static function init($config = []) - { - if (isset($config['prefix'])) { - self::$prefix = $config['prefix']; - } - if ($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_trans_sid'])) { - ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); - } - 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'])) { - // 读取session驱动 - $class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\session\\driver\\') . ucwords($config['type']); - // 检查驱动类 - if (!session_set_save_handler(new $class())) { - throw new Exception('error session handler', 11700); - } - } - // 启动session - if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) { - ini_set('session.auto_start', 0); - session_start(); - } - - } - - /** - * session设置 - * @param string $name session名称 - * @param mixed $value session值 - * @param string $prefix 作用域(前缀) - * @return void - */ - public static function set($name, $value = '', $prefix = '') - { - $prefix = $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; - } - } - - /** - * session获取 - * @param string $name session名称 - * @param string $prefix 作用域(前缀) - * @return mixed - */ - public static function get($name = '', $prefix = '') - { - $prefix = $prefix ? $prefix : self::$prefix; - if ('' == $name) { - // 获取全部的session - $value = $prefix ? $_SESSION[$prefix] : $_SESSION; - } elseif ($prefix) { - // 获取session - 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; - } - - /** - * 删除session数据 - * @param string $name session名称 - * @param string $prefix 作用域(前缀) - * @return void - */ - public static function delete($name, $prefix = '') - { - $prefix = $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]); - } - } - } - - /** - * 清空session数据 - * @param string $prefix 作用域(前缀) - * @return void - */ - public static function clear($prefix = '') - { - $prefix = $prefix ? $prefix : self::$prefix; - if ($prefix) { - unset($_SESSION[$prefix]); - } else { - $_SESSION = []; - } - } - - /** - * 判断session数据 - * - * @param string $name session名称 - * @param string $prefix - * - * @return bool - * @internal param mixed $value session值 - */ - public static function has($name, $prefix = '') - { - $prefix = $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]); - } - } - - /** - * 暂停session - * @return void - */ - public static function pause() - { - // 暂停session - session_write_close(); - } - - /** - * 启动session - * @return void - */ - public static function start() - { - session_start(); - } - - /** - * 销毁session - * @return void - */ - public static function destroy() - { - $_SESSION = []; - session_unset(); - session_destroy(); - } - - /** - * 重新生成session_id - * @return void - */ - private static function regenerate() - { - session_regenerate_id(); - } - -} + +// +---------------------------------------------------------------------- +namespace think; + +class Session { + protected static $prefix = ''; + + /** + * 设置或者获取session作用域(前缀) + * + * @param string $prefix + * @return string|void + */ + public static function prefix($prefix = '') { + if (empty ( $prefix ) && $prefix !== null) { + return self::$prefix; + } else { + self::$prefix = $prefix; + } + } + + /** + * session初始化 + * + * @param array $config + * @return void + */ + public static function init($config = []) { + if (isset ( $config ['use_trans_sid'] )) { + ini_set ( 'session.use_trans_sid', $config ['use_trans_sid'] ? 1 : 0 ); + } + + // 启动session + if (! empty ( $config ['auto_start'] ) && PHP_SESSION_ACTIVE != session_status ()) { + ini_set ( 'session.auto_start', 0 ); + session_start (); + } + + if (isset ( $config ['prefix'] )) { + self::$prefix = $config ['prefix']; + } + if ($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'] )) { + // 读取session驱动 + $class = (! empty ( $config ['namespace'] ) ? $config ['namespace'] : '\\think\\session\\driver\\') . ucwords ( $config ['type'] ); + + // 检查驱动类 + if (! class_exists ( $class ) || ! session_set_save_handler ( new $class () )) { + throw new \think\Exception ( 'error session handler', 11700 ); + } + } + } + + /** + * session设置 + * + * @param string $name + * session名称 + * @param mixed $value + * session值 + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function set($name, $value = '', $prefix = '') { + $prefix = $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; + } + } + + /** + * session获取 + * + * @param string $name + * session名称 + * @param string $prefix + * 作用域(前缀) + * @return mixed + */ + public static function get($name = '', $prefix = '') { + $prefix = $prefix ? $prefix : self::$prefix; + if ('' == $name) { + // 获取全部的session + $value = $prefix ? $_SESSION [$prefix] : $_SESSION; + } elseif ($prefix) { + // 获取session + 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; + } + + /** + * 删除session数据 + * + * @param string $name + * session名称 + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function delete($name, $prefix = '') { + $prefix = $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] ); + } + } + } + + /** + * 清空session数据 + * + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function clear($prefix = '') { + $prefix = $prefix ? $prefix : self::$prefix; + if ($prefix) { + unset ( $_SESSION [$prefix] ); + } else { + $_SESSION = [ ]; + } + } + + /** + * 判断session数据 + * + * @param string $name + * session名称 + * @param string $prefix + * + * @return bool + * @internal param mixed $value session值 + */ + public static function has($name, $prefix = '') { + $prefix = $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] ); + } + } + + /** + * 暂停session + * + * @return void + */ + public static function pause() { + // 暂停session + session_write_close (); + } + + /** + * 启动session + * + * @return void + */ + public static function start() { + session_start (); + } + + /** + * 销毁session + * + * @return void + */ + public static function destroy() { + $_SESSION = [ ]; + session_unset (); + session_destroy (); + } + + /** + * 重新生成session_id + * + * @return void + */ + private static function regenerate() { + session_regenerate_id (); + } +} diff --git a/library/think/SessionTest.php b/library/think/SessionTest.php deleted file mode 100644 index 7dee500a..00000000 --- a/library/think/SessionTest.php +++ /dev/null @@ -1,150 +0,0 @@ -object = new Session; - } - - /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. - */ - protected function tearDown() - { - } - - /** - * @covers think\Session::prefix - * @todo Implement testPrefix(). - */ - public function testPrefix() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::init - * @todo Implement testInit(). - */ - public function testInit() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::set - * @todo Implement testSet(). - */ - public function testSet() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::get - * @todo Implement testGet(). - */ - public function testGet() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::delete - * @todo Implement testDelete(). - */ - public function testDelete() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::clear - * @todo Implement testClear(). - */ - public function testClear() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::has - * @todo Implement testHas(). - */ - public function testHas() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::pause - * @todo Implement testPause(). - */ - public function testPause() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::start - * @todo Implement testStart(). - */ - public function testStart() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @covers think\Session::destroy - * @todo Implement testDestroy(). - */ - public function testDestroy() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } -} From 12379d08a0fbe57525a1f265fd20c3017140415e Mon Sep 17 00:00:00 2001 From: zhylninc Date: Thu, 14 Jan 2016 22:29:53 +0800 Subject: [PATCH 3/3] 1. fix session_start() step to end 2. fix format to PSR2 --- library/think/Session.php | 495 ++++++++++++++++++++------------------ 1 file changed, 256 insertions(+), 239 deletions(-) diff --git a/library/think/Session.php b/library/think/Session.php index 42f549b5..217a33dc 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -10,243 +10,260 @@ // +---------------------------------------------------------------------- namespace think; -class Session { - protected static $prefix = ''; - - /** - * 设置或者获取session作用域(前缀) - * - * @param string $prefix - * @return string|void - */ - public static function prefix($prefix = '') { - if (empty ( $prefix ) && $prefix !== null) { - return self::$prefix; - } else { - self::$prefix = $prefix; - } - } - - /** - * session初始化 - * - * @param array $config - * @return void - */ - public static function init($config = []) { - if (isset ( $config ['use_trans_sid'] )) { - ini_set ( 'session.use_trans_sid', $config ['use_trans_sid'] ? 1 : 0 ); - } - - // 启动session - if (! empty ( $config ['auto_start'] ) && PHP_SESSION_ACTIVE != session_status ()) { - ini_set ( 'session.auto_start', 0 ); - session_start (); - } - - if (isset ( $config ['prefix'] )) { - self::$prefix = $config ['prefix']; - } - if ($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'] )) { - // 读取session驱动 - $class = (! empty ( $config ['namespace'] ) ? $config ['namespace'] : '\\think\\session\\driver\\') . ucwords ( $config ['type'] ); - - // 检查驱动类 - if (! class_exists ( $class ) || ! session_set_save_handler ( new $class () )) { - throw new \think\Exception ( 'error session handler', 11700 ); - } - } - } - - /** - * session设置 - * - * @param string $name - * session名称 - * @param mixed $value - * session值 - * @param string $prefix - * 作用域(前缀) - * @return void - */ - public static function set($name, $value = '', $prefix = '') { - $prefix = $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; - } - } - - /** - * session获取 - * - * @param string $name - * session名称 - * @param string $prefix - * 作用域(前缀) - * @return mixed - */ - public static function get($name = '', $prefix = '') { - $prefix = $prefix ? $prefix : self::$prefix; - if ('' == $name) { - // 获取全部的session - $value = $prefix ? $_SESSION [$prefix] : $_SESSION; - } elseif ($prefix) { - // 获取session - 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; - } - - /** - * 删除session数据 - * - * @param string $name - * session名称 - * @param string $prefix - * 作用域(前缀) - * @return void - */ - public static function delete($name, $prefix = '') { - $prefix = $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] ); - } - } - } - - /** - * 清空session数据 - * - * @param string $prefix - * 作用域(前缀) - * @return void - */ - public static function clear($prefix = '') { - $prefix = $prefix ? $prefix : self::$prefix; - if ($prefix) { - unset ( $_SESSION [$prefix] ); - } else { - $_SESSION = [ ]; - } - } - - /** - * 判断session数据 - * - * @param string $name - * session名称 - * @param string $prefix - * - * @return bool - * @internal param mixed $value session值 - */ - public static function has($name, $prefix = '') { - $prefix = $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] ); - } - } - - /** - * 暂停session - * - * @return void - */ - public static function pause() { - // 暂停session - session_write_close (); - } - - /** - * 启动session - * - * @return void - */ - public static function start() { - session_start (); - } - - /** - * 销毁session - * - * @return void - */ - public static function destroy() { - $_SESSION = [ ]; - session_unset (); - session_destroy (); - } - - /** - * 重新生成session_id - * - * @return void - */ - private static function regenerate() { - session_regenerate_id (); - } +class Session +{ + + protected static $prefix = ''; + + /** + * 设置或者获取session作用域(前缀) + * + * @param string $prefix + * @return string|void + */ + public static function prefix($prefix = '') + { + if (empty($prefix) && $prefix !== null) { + return self::$prefix; + } else { + self::$prefix = $prefix; + } + } + + /** + * session初始化 + * + * @param array $config + * @return void + */ + public static function init($config = []) + { + $isDoStart = false; + if (isset($config['use_trans_sid'])) { + ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); + } + + // 启动session + 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 ($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'])) { + // 读取session驱动 + $class = (! empty($config['namespace']) ? $config['namespace'] : '\\think\\session\\driver\\') . ucwords($config['type']); + + // 检查驱动类 + if (! class_exists($class) || ! session_set_save_handler(new $class())) { + throw new \think\Exception('error session handler', 11700); + } + } + if ($isDoStart) { + session_start(); + } + } + + /** + * session设置 + * + * @param string $name + * session名称 + * @param mixed $value + * session值 + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function set($name, $value = '', $prefix = '') + { + $prefix = $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; + } + } + + /** + * session获取 + * + * @param string $name + * session名称 + * @param string $prefix + * 作用域(前缀) + * @return mixed + */ + public static function get($name = '', $prefix = '') + { + $prefix = $prefix ? $prefix : self::$prefix; + if ('' == $name) { + // 获取全部的session + $value = $prefix ? $_SESSION[$prefix] : $_SESSION; + } elseif ($prefix) { + // 获取session + 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; + } + + /** + * 删除session数据 + * + * @param string $name + * session名称 + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function delete($name, $prefix = '') + { + $prefix = $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]); + } + } + } + + /** + * 清空session数据 + * + * @param string $prefix + * 作用域(前缀) + * @return void + */ + public static function clear($prefix = '') + { + $prefix = $prefix ? $prefix : self::$prefix; + if ($prefix) { + unset($_SESSION[$prefix]); + } else { + $_SESSION = []; + } + } + + /** + * 判断session数据 + * + * @param string $name + * session名称 + * @param string $prefix + * + * @return bool + * @internal param mixed $value session值 + */ + public static function has($name, $prefix = '') + { + $prefix = $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]); + } + } + + /** + * 暂停session + * + * @return void + */ + public static function pause() + { + // 暂停session + session_write_close(); + } + + /** + * 启动session + * + * @return void + */ + public static function start() + { + session_start(); + } + + /** + * 销毁session + * + * @return void + */ + public static function destroy() + { + $_SESSION = []; + session_unset(); + session_destroy(); + } + + /** + * 重新生成session_id + * + * @return void + */ + private static function regenerate() + { + session_regenerate_id(); + } }