Skip to content

Commit

Permalink
Session类库优化
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Jan 5, 2014
1 parent 33c37d4 commit 24f1c8d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
39 changes: 29 additions & 10 deletions modules/session/driver/cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
44 changes: 29 additions & 15 deletions modules/session/driver/default.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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();
}
Expand All @@ -102,7 +116,7 @@ public function session_id()
*/
public function destroy()
{
if ( session_id() !== '' )
if (session_id() !== '')
{
$name = session_name();

Expand Down
5 changes: 5 additions & 0 deletions modules/session/session.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 24f1c8d

Please sign in to comment.