我正在使用Codeigniter 2,并希望将站点上当前用户的数量跟踪到数据库中。我知道这可以使用内置的会话类来实现,但我不确定我做错了什么 - 我在MySQL中创建了ci_sessions表并在配置中设置了以下内容 - 我是否需要对其进行任何其他调整当有人访问网站时自动将会话记录到数据库中?
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
答案 0 :(得分:3)
<强>模型强>
class online_users_mod extends CI_Model
{
function __construct(){
parent::__construct();
}
//getting all session data
function get_all_session_data(){
$query=$this->db->select('user_data')->get('ci_sessions');
return $query;
}
}
<强>控制器强>
class online_users_lib extends CI_Controller{
//put your code here
/**
* CodeIgniter global
*
* @var string
**/
protected $ci;
public function __construct() {
$this->ci =& get_instance();
$this->ci->load->model('online_users_mod');
}
//getting all online session data
function get_all_session_data(){
$query=$this->ci->online_users_mod->get_all_session_data();
$user = array();
/* array to store the user data we fetch */
$j=0;$k=0;
foreach ($query->result() as $row)
{
$udata = unserialize($row->user_data);
/* put data in array using username as key */
if(isset($udata['yourlogin_data']))
{
if($udata['yourlogin_data']['User_cat']==2)
{
$user[$j]['yourlogin_data'] = $udata['yourlogin_data'];
$j++;
}
}
}
return $user ;
}
}