我想获取当前会话ID以在所有页面中使用它。我想在标头中传递会话数据。我可以将我的会话存储在ci_sessions
表中。但是,如何从ci_sessions
检索当前登录用户名/ sesson_id。
这是我的ci_sessions代码。
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
这是会话设置代码
if($this->form_validation->run())
{
$username = $this->input->post('login_username');
$password = $this->input->post('login_password');
// ip address
//$ip_address= $this->user_activity->get_client_ip();
//Retrieving session data and other data
//$captcha_code=$_SESSION['captcha'];
//$user_agent=$_SERVER['HTTP_USER_AGENT'];
//call the model for auth
$this->load->Model('Auth_model');
if($this->Auth_model->login($username, $password)){
$data = array(
'client_username' => $username,
'is_logged_in' => true
);
$this->session->set_userdata($data);
$this->load->view('include/header', $data);
//$this->session->set_flashdata('item', $datav['name']);
}
else{
echo'something went wrong';
}
我正在使用pop dailog登录和注册,所以这是我的ajax。
<script>
$("input#login_btn").on('click',function(event){
event.preventDefault();
var x = document.forms["loginform"]["login_username"].value;
var x1 = document.forms["loginform"]["login_password"].value;
var y = document.getElementById('login-error');
var z = document.getElementById('password-error');
if (x == "") { y.style.display ='block'; }else{ y.style.display ='none'; }
if (x1 == "") { z.style.display ='block'; }else{ z.style.display ='none'; }
var d = document.forms["loginform"]["login_username"].value;
var d1 = document.forms["loginform"]["login_password"].value;
if(d != '' && d1 != ''){
var url = $(this).attr('action');
var method = $(this).attr('method');
var data = $(this).serialize();
$.ajax({
url:url,
type:method,
data:data
}).done(function(data){
if(data !=='')
{
if(d !== '' && d1 != '' ){
//$("#login_fail").show('fast');
//$("#login_fail").effect( "shake" );
$('#loginform')[0].reset();
//var url = $('#current_loginurl').val();
//window.location.href=url;
var url = $('#current_loginurl').val();
window.location.href=url;
}
}
else
{
var url = $('#current_loginurl').val();
window.location.href=url;
throw new Error('go');
}
});
$( "div" ).each(function( index ) {
var cl = $(this).attr('class');
if(cl =='')
{
$(this).hide();
}
});
}
});
</script>
答案 0 :(得分:0)
从下面的表格中查找current session
。获取order by
订单descending
中的记录limit
ID。
$this->db->order_by('id', 'DESC');
$this->db->limit(1);
$last_session = $this->db->get('ci_sessions')->row_array();
print_r($last_session['data']);//prints current session data
<强>更新强>
但获取当前用户session id
的最简单方法是使用session
库。就像这样..
$this->load->library('session');
$id = $this->session->userdata('id');//id is session variable that you set
echo $id;