会话不会regestring 3变量
问题描述:
我已经在codeigniter中创建了一个登录系统。它工作正常,但是当我登录时,它只注册了2个变量,它们是登录名和密码,但没有显示会话中user_type的第三个变量。我尝试了太多,但我不明白什么是错的。会话不会regestring 3变量
型号代码
function login($username, $password) {
//create query to connect user login database
$this->db->select('user_id, username, password','user_type');
$this->db->from('login');
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->where('status',1);
$this->db->limit(1);
//get query and processing
echo $query = $this->db->get();
if($query->num_rows() == 1) {
return $query->result(); //if data is true
} else {
return false; //if data is wrong
}
}
varifying代码控制器
function index() {
$this->form_validation->set_rules('username', 'username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login');
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
function check_database($password) {
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->login->login($username, $password);
if($result) {
$sess_array = array();
foreach($result as $row) {
//create the session
$sess_array = array('user_id' => $row->user_id,
'username' => $row->username,
'user_type'=> $row->user_type
);
//set session with value from database
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
} else {
//if form validate false
$this->form_validation->set_message('check_database', '<p style="color: red;">اسم المستخدم/كلمة المرور غير صحيحة ! </p>');
return FALSE;
}
}
会话变量代码
function session_start(){
/*---------------------------- session start here */
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['id'] = $session_data['user_id'];
$data['user_type'] = $session_data['user_type'];
} else {
//If no session, redirect to login page
redirect('c_login', 'refresh');
}
/* ---------------------- session start ends */
答
因为你是不是在你的选择查询选择user_type
。
更改此:
$this->db->select('user_id, username, password','user_type');
有了这个:
$this->db->select('user_id, username, password,user_type');
,并请你告诉我,什么是您以前的查询视为'user_type'作为第二个参数这两个查询之间的区别 –
。这就是为什么它没有在你的查询中被选中。 –
ow是我的错误感谢朋友 –