1
votes

I am working with CodeIgniter. However, when I use session, there is a problem occured. The problem is: I try to store user_email in session. Then I redirect to another controller. In this case it works perfectly. However, when I am adding one new user to the database, then I try to login, store the session, and redirect to another controller, the session is lost. Anyone could help solving my problem? Thanks in advance

Here is the code:

cotroller1

public function make_session($parameter)
{
    $this->session->set_userdata('user', $parameter );
}

// Fungsi untuk menggenerate page login
public function do_login()
{
    // Memanggil page untuk proccess login 
    // grab user input
    $email = $this->input->post('user_email');
    $password = $this->input->post('user_password');

    // Menyimpan variable first name    
    $this->load->model('login_model');
    $data['user'] = $this->login_model->get_user( $email, $password );

    // Set session daripada user
    if( $data['user'] != NULL )
    {
        foreach ($data['user'] as $ad):
            $this->make_session( $ad['user_email'] );
        endforeach;
    }

    $this->index();
}

controller2

public function index(){
        $data['session_u_email'] = $this->session->userdata('user');
        $this->load->model('main_model');

        // Mendapatkan nama dari si user
        $data['fname'] = $this->main_model->get_user_fname( $data['session_u_email'] );

        // Mendapatkan role dari user
        $data['role_id'] = $this->main_model->get_role_id( $data['session_u_email'] );

        // Mendapatkan user id dahulu
        $this->load->model('input_model');
        $id = $this->input_model->get_user_id( $data['session_u_email'] );

        // Setelah dapat id, nendapatkan jumlah input dari si user
        $data['input_number'] = $this->main_model->get_input_number( $id );

        //Mendapatkan total agen
        $data['tot'] = $this->main_model->get_total_agen();
        $data['date'] = date("F j, Y, g:i a");

        $this->load->view('template/header', $data);
        $this->load->view('main_v', $data);
        $this->load->view('template/footer');


}

model

public function get_user($u_email, $u_password)
        {
            // Prep the query
            $this->db->where('user_email', $u_email);
            $this->db->where('user_password', sha1($u_password));

            // Run the query
            $query = $this->db->get('user');

            if($query->num_rows() == 1)
            {
                $query->result_array();
            }
        }
1
How on earth do you expect people to help you without any code posted?Mihai
so sorry about that. here is my codeuser1814734

1 Answers

0
votes

Your model does not return anything at all - this is the same as returning null, which results in no session being created, as no user will ever be found.

You should thus add a return to your model likewise:

public function get_user($u_email, $u_password)
    {
        // Prep the query
        $this->db->where('user_email', $u_email);
        $this->db->where('user_password', sha1($u_password));

        // Run the query
        $query = $this->db->get('user');

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