CI的Session并不是原生的session,正是我前面所有的cookie based session,另外,CI可以根据用户选择配置是否将session存入数据库中,本人很喜欢这个功能,还有就是“闪出数据”的功能,既闪出数据只是对下次服务器请求可以,之后就会被自动清除。常见使用方法有:
$this->session->set_userdata('some_name', 'some_value'); //设置session数据
$this->session->userdata('item'); //获取session数据
$this->session->unset_userdata('some_name'); //删除session数据
$this->session->sess_destroy(); //销毁session数据
$this->session->set_flashdata('item', 'value'); //设置闪存数据
$this->session->flashdata('item'); //获取闪存数据
$this->session->keep_flashdata('item'); //保留闪存数据
复制代码 代码如下:
/**
* CI是 session based cookie
*/
class CI_Session {
var $sess_encrypt_cookie = FALSE; //是否对session加密
var $sess_use_database = FALSE; //是否将session存入数据库
var $sess_table_name = ''; //session存入数据的表名
var $sess_expiration = 7200; //session的过期时间
var $sess_expire_on_close = FALSE; //当浏览器窗口关闭时是否自动使session过期
var $sess_match_ip = FALSE;//是否通过用户的IP地址来读取 session 的数据
var $sess_match_useragent = TRUE; //是否要按照对应的 User Agent 来读取 session 数据。
var $sess_cookie_name = 'ci_session'; //cookie名称
var $cookie_prefix = ''; //cookie前缀
var $cookie_path = ''; //cookie路径
var $cookie_domain = ''; //cookie作用域
var $cookie_secure = FALSE; //是否在安全的https协议下才有效
var $sess_time_to_update = 300; //session cookie多久更新一次
var $encryption_key = ''; //加密key
var $flashdata_key = 'flash';
var $time_reference = 'time';
var $gc_probability = 5; //回收session的能力
var $userdata = array(); //用户session数据保存变量
var $CI; //CI超级句柄
var $now; //当前时间
public function __construct($params = array())
{
log_message('debug', "Session Class Initialized");
// 获取CI超级类
$this->CI =& get_instance();
// 获取config文件中的配置数据
foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)
{
$this->$key = (isset($params[$key])) "Session routines successfully run");
}
// --------------------------------------------------------------------
/**
* 读取session数据
*/
function sess_read()
{
// 获取session
$session = $this->CI->input->cookie($this->sess_cookie_name);
// 没有session 拜拜
if ($session === FALSE)
{
log_message('debug', 'A session cookie was not found.');
return FALSE;
}
// 如果加密了cookie
if ($this->sess_encrypt_cookie == TRUE)
{
$session = $this->CI->encrypt->decode($session);
}
else
{
// encryption was not used, so we need to check the md5 hash
$hash = substr($session, strlen($session)-32); // get last 32 chars
$session = substr($session, 0, strlen($session)-32);
// Does the md5 hash match"H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
}
else
{
$time = time();
}
return $time;
}
// --------------------------------------------------------------------
/**
* 写入session cookie
*
*/
function _set_cookie($cookie_data = NULL)
{
if (is_null($cookie_data))
{
$cookie_data = $this->userdata;
}
// 序列化数组
$cookie_data = $this->_serialize($cookie_data);
//加密数据
if ($this->sess_encrypt_cookie == TRUE)
{
$cookie_data = $this->CI->encrypt->encode($cookie_data);
}
else
{
// if encryption is not used, we provide an md5 hash to prevent userside tampering
$cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
}
//sess_expire_on_close为TRUE则,浏览器关闭,session失效
$expire = ($this->sess_expire_on_close === TRUE) "last_activity < {$expire}");
$this->CI->db->delete($this->sess_table_name);
log_message('debug', 'Session garbage collection performed.');
}
}
}