From 24f1c8dd64f4440c8754cbb06034458835ca424b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=BC=E5=90=B8=E4=BA=8C=E6=B0=A7=E5=8C=96=E7=A2=B3?= Date: Sun, 5 Jan 2014 21:41:15 +0800 Subject: [PATCH] =?UTF-8?q?Session=E7=B1=BB=E5=BA=93=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/session/driver/cache.class.php | 39 +++++++++++++++------ modules/session/driver/default.class.php | 44 ++++++++++++++++-------- modules/session/session.class.php | 5 +++ 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/modules/session/driver/cache.class.php b/modules/session/driver/cache.class.php index 369fcf8..3196ff1 100644 --- a/modules/session/driver/cache.class.php +++ b/modules/session/driver/cache.class.php @@ -53,29 +53,48 @@ public function driver() /** * Create a new session. * - * @param array variables to set after creation * @return void */ - public function create($vars = null) + public function create() { - $cookieconfig = Core::config('cookie'); + $cookie_config = Core::config('cookie'); $_SESSION = array(); - $sid = Core::cookie()->get($this->session_name); - if ( !$sid || !Session::check_session_id($sid) ) + if (Session::$config['type']=='url') + { + $sid = HttpIO::GET($this->session_name); + } + else + { + $sid = HttpIO::COOKIE($this->session_name); + } + + if (!$sid || !Session::check_session_id($sid)) { $sid = Session::create_session_id(); - # 将session存入cookie - Core::cookie()->set($this->session_name, $sid, null, $cookieconfig['path'], $cookieconfig['domain'], $cookieconfig['secure'], $cookieconfig['httponly']); + if (Session::$config['type']=='cookie') + { + # 将session存入cookie + Core::cookie()->set($this->session_name, $sid, null, $cookie_config['path'], $cookie_config['domain'], $cookie_config['secure'], $cookie_config['httponly']); + } + } + + # 添加URL处理自动追加SESSION ID参数 + if (Session::$config['type']=='url') + { + Core::add_url_args(Session::$config['name'], $sid); } - $this->driver()->session_mode(true); + # 调试模式设置Session模式,避免开启缓存模式时获取不到Session + if (IS_DEBUG)$this->driver()->session_mode(true); + $_SESSION = $this->driver()->get($sid); - $this->driver()->session_mode(false); - if ( !is_array($_SESSION) ) + if (IS_DEBUG)$this->driver()->session_mode(false); + + if (!is_array($_SESSION)) { $_SESSION = array(); } diff --git a/modules/session/driver/default.class.php b/modules/session/driver/default.class.php index 2043c1e..4d7aff6 100644 --- a/modules/session/driver/default.class.php +++ b/modules/session/driver/default.class.php @@ -23,7 +23,7 @@ public function __construct() { @ini_set('session.gc_probability', (int)Session::$config['gc_probability']); @ini_set('session.gc_divisor', 100); - @ini_set('session.gc_maxlifetime', (Session::$config['expiration'] == 0)?2592000:Session::$config['expiration']); + @ini_set('session.gc_maxlifetime', (Session::$config['expiration']==0)?2592000:Session::$config['expiration']); // session保存接口 if (isset(Session::$config['save_handler']) && Session::$config['save_handler']) @@ -49,40 +49,54 @@ public function __construct() */ public function create() { - if ( preg_match('#^(?=.*[a-z])[a-z0-9_]++$#iD', Session::$config['name']) ) - { - session_name(Session::$config['name']); - } + session_name(Session::$config['name']); + $this->destroy(); - $cookieconfig = Core::config('cookie'); + $cookie_config = Core::config('cookie'); # 这里对IP+非80端口的需要特殊处理下,经试验,当这种情况下,设置session id的cookie的话会失败 - if (preg_match('#^([0-9]+.[0-9]+.[0-9]+.[0-9]+):[0-9]+$#',$cookieconfig['domain'],$m)) + if (preg_match('#^([0-9]+.[0-9]+.[0-9]+.[0-9]+):[0-9]+$#', $cookie_config['domain'],$m)) { # IP:PORT 方式 - $cookieconfig['domain'] = $m[1]; + $cookie_config['domain'] = $m[1]; } - $sname = session_name(); - if (isset($_COOKIE[$sname]) && $_COOKIE[$sname]) + $s_name = session_name(); + if (Session::$config['type']=='url') { - $old_sid = $_COOKIE[$sname]; + $old_sid = HttpIO::COOKIE($s_name); + } + else + { + $old_sid = HttpIO::COOKIE($s_name); + } + if ($old_sid) + { # 校验Session ID if (!Session::check_session_id($old_sid)) { # 如果检验的Session ID不合法,则重新生成一个 - session_id( Session::create_session_id() ); + session_id(Session::create_session_id()); } } else { # 设置Session ID - session_id( Session::create_session_id() ); + session_id(Session::create_session_id()); } - session_set_cookie_params(Session::$config['expiration'], $cookieconfig['path'], $cookieconfig['domain'], $cookieconfig['secure'], $cookieconfig['httponly']); + # Session ID 通过uri传递 + if (Session::$config['type']=='url') + { + @ini_set('session.use_cookies', 0); + @ini_set('session.use_only_cookies', 0); + } + else + { + session_set_cookie_params($cookie_config['httponly']?0:Session::$config['expiration'], $cookie_config['path'], $cookie_config['domain'], $cookie_config['secure'], $cookie_config['httponly']); + } session_start(); } @@ -102,7 +116,7 @@ public function session_id() */ public function destroy() { - if ( session_id() !== '' ) + if (session_id() !== '') { $name = session_name(); diff --git a/modules/session/session.class.php b/modules/session/session.class.php index 184fe2c..cd43877 100644 --- a/modules/session/session.class.php +++ b/modules/session/session.class.php @@ -96,6 +96,11 @@ public function __construct($vars = null) $this->driver = new Session_Driver_Default(); } + if (!isset(Session::$config['type']) || Session::$config['type']!='url') + { + Session::$config['type'] = 'cookie'; + } + if (IS_DEBUG) { if ($is_debug)