设置会话以使用codeigniter

时间:2016-02-18 03:53:18

标签: php codeigniter session user-data

好的我正在尝试构建一个用户配置文件,其中视图中显示了first_name,password和username。

因为在模型中设置了密码和用户名,所以很容易在控制器中设置会话用户数据,然后在视图中显示它。

我怎么能设置一个会话来获取first_name而不将它放在验证函数中?

谢谢,我是codeigniter和MVC的新手, 谢谢你的帮助。

模型

function validate()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('membership');

    if($query->num_rows() == 1)
    {
        return true;
    }
}

CONTROLER

function validate_credentials()
{
    $this->load->model('member_model');
    $query = $this->member_model->validate();

    if ($query) // if user cred validate the user session start
    {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),

            'is_logged_in' => true
        );

        $this->session->set_userdata($data);

        redirect('members/members_area');
    } else {
        $this->index();
        echo 'Incorrect Password or Username';
    }
}

查看

<h2>Welcome Back, <?php echo $this->session->userdata('username'); ?>!</h2>
<h2>your password, <?php echo $this->session->userdata('password'); ?>!</h2>

1 个答案:

答案 0 :(得分:1)

验证功能

function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');

if($query->num_rows() == 1)
{
    return $query->row();
}
else
{
return false;
}
}

在控制器

function validate_credentials()
{
$this->load->model('member_model');
$query = $this->member_model->validate();

if ($query) // if user cred validate the user session start
{
    $data = array(
        'username' => $query->username,
        'password' => $query->password,
        'first_name'=>$query->firstname
        'is_logged_in' => true
    );

    $this->session->set_userdata($data);

    redirect('members/members_area');
 } else {
    $this->index();
    echo 'Incorrect Password or Username';
}
}