会话不 regestring 3 变量
session not regestring the 3 variable
我已经在 codeigniter 中创建了一个登录系统。它工作正常,但是当我登录时,它只注册了 2 个变量,即登录名和密码,但没有显示会话中的第三个变量 user_type。我试了太多,但我不明白哪里出了问题。
型号代码
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
}
}
变码控制器
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 */
因为您没有在 select 查询中 selecting user_type
。
改变这个:
$this->db->select('user_id, username, password','user_type');
有了这个:
$this->db->select('user_id, username, password,user_type');
我已经在 codeigniter 中创建了一个登录系统。它工作正常,但是当我登录时,它只注册了 2 个变量,即登录名和密码,但没有显示会话中的第三个变量 user_type。我试了太多,但我不明白哪里出了问题。
型号代码
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
}
}
变码控制器
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 */
因为您没有在 select 查询中 selecting user_type
。
改变这个:
$this->db->select('user_id, username, password','user_type');
有了这个:
$this->db->select('user_id, username, password,user_type');