Beep
Beep

Reputation: 2823

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>

Upvotes: 1

Views: 233

Answers (1)

Gopalakrishnan
Gopalakrishnan

Reputation: 957

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

Upvotes: 1

Related Questions