简体   繁体   中英

setting sessions to use codeigniter

OK I am trying to build a user profile where there first_name, password and username are shown in the view.

because password and username are set in the model its very easy to set the session userdata in the controller then have it shown in the view.

how could I also set a sessions to get first_name without puting it in the validation function ?

thank you, im new to codeigniter and MVC, thanks for any help.

Model

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';
    }
}

view

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

Validation Function

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;
}
}

In Controller

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';
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM