【发布时间】:2017-01-05 06:04:41
【问题描述】:
我是 CodeIgniter 的新手,我创建了一个登录系统,其中我有一个会话。在这里,我可以通过使用 POST 获取帐户的用户名并将其放入会话数组中。但是,我还想从数据库中获取它的其他数据,例如它的名字、姓氏等,并将其也放入会话中。为了让我实现它,正确的语法是什么?
控制器:
function validate_login()
{
$this->load->model('model_accounts');
$valid = $this->model_accounts->validate();
$isAdmin = $this->model_accounts->check_role();
$isActive = $this->model_accounts->check_active();
if($valid && $isAdmin && $isActive) // Active Admin
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('admin_ticketing');
}
else if(($valid && $isActive) && $isAdmin == false) // Active User
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('user_home');
}
else if(($valid && $isAdmin) && $isActive == false) //Deactivated Admin
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/admindeact');
}
else if($valid && ($isActive && $isAdmin) == false) //Deactivated User
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/userdeact');
}
else if($valid == false) //Invalid Account
{
$data['main_content'] = 'view_login';
$data['message'] = "The username and password you entered did not match our records. Please double-check and try again. ";
$this->load->view('includes/login_template', $data);
}
}
型号:
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('accounts');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
function check_role()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$this->db->where('role', 1);
$query = $this->db->get('accounts');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
function check_active()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$this->db->where('isActive', 1);
$query = $this->db->get('accounts');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
【问题讨论】:
-
您可以在会话中创建一个数组并存储您需要的所有数据,如下所示 $this->session->set_userdata('sessionarr',$resultset);
-
我已经创建了这个数组
$data = array( 'username' => $this->input->post('username'), 'password' => $this->input->post('password'), 'is_logged_in' => true );但我想从数据库中获取上面用户名的名字。我想知道怎么做 -
使用提供的用户名和密码从数据库中获取用户数据,并像您说的那样再次存储...
eg: if($query->num_rows() == 1) { return $query->result; //it contains all userdata } -
@coderszx 检查以下答案。
标签: php codeigniter session