Yasitha
Yasitha

Reputation: 911

Codeigniter session variable not working

I'm Using codeigniter frame work and i'm having some problems with session.

in my login form i ask for username and password, then if supplied credentials are valid, then i put them in to a session variable as this.

$user_session_data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session_data);

everything works well before. but today i add codes in to some pages to set some error messages to session.

$error_array[]="There Is An Error When Inserting Data In To The Database";
$this->session->set_userdata(array('upload_errors' => $error_array));

after this done. i can log into the admin area. but i cant do any thing it always ask me to login again. i think the second code is overriding the loging session data.

any idea to solve this error?

Upvotes: 0

Views: 6044

Answers (2)

Himanshu Pandey
Himanshu Pandey

Reputation: 1270

please use this code to set session in codeigniter

$this->session->set_userdata('user_session', $user_session_data);

Upvotes: 4

KRUNAL DOSHI
KRUNAL DOSHI

Reputation: 81

You are calling $this->session->set_userdata() it twice , this will remove all old data and set new once.

So if you are using it for error you can use below code

If you want to show error codeigniter session library have function to do that

$this->session->set_flashdata('item', 'value');

Set value using above code and get that value using

$this->session->flashdata('item');

Reference : http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Upvotes: 0

Related Questions