Does Setting Session in Model is bad practice? <p>I do some database transaction and the set <strong>$new_user = TRUE</strong> like this</p> <p>Model Code:</p> <pre><code>$data_insert = array ( 'username' =&gt; $username_post, 'password'=&gt;$username_post, ); $this-&gt;db-&gt;insert('Tenant', $data_insert); $new_tenant_id = $this-&gt;db-&gt;insert_id(); //CREATE TABLE FOR THAT DISTRIBUTOR $this-&gt;dbforge-&gt;add_field( array( 'id' =&gt; array( 'type' =&gt; 'INT', 'constraint' =&gt; 10, 'unsigned' =&gt; TRUE, 'auto_increment' =&gt; TRUE ), 'site_key' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', ), 'display_name' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', 'null' =&gt; TRUE ), 'ext' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '50', 'null' =&gt; TRUE ), 'auth_user' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', 'null' =&gt; TRUE ), 'password' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '128', 'null' =&gt; TRUE, ), 'base_ini_id' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '50', 'null' =&gt; TRUE ), 'md_user' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '128', 'null' =&gt; TRUE ), 'uc_user' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '50', 'null' =&gt; TRUE ), 'uc_password' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', 'null' =&gt; TRUE ), 'comments' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '200', 'null' =&gt; TRUE ), 'custom_ini_filename' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', 'null' =&gt; TRUE ), 'email' =&gt; array( 'type' =&gt; 'VARCHAR', 'constraint' =&gt; '100', 'null' =&gt; TRUE ), )); $this-&gt;dbforge-&gt;add_key('id', TRUE); if (!$this-&gt;db-&gt;table_exists('table_name')) { $this-&gt;dbforge-&gt;create_table($usertable); } //TABLE CREATED NOW ADD SOME DATA $insert_data = array ( 'site_key' =&gt;$site_post, 'tenant_id'=&gt;$new_tenant_id ); //TENANT CREATED AND THE SITE BY HIM IS ADDED TO DATABASE $query = $this-&gt;db-&gt;insert('MLCSites',$insert_data); $new_user = TRUE; </code></pre> <p>Now if i have the user in database then $validate is set to TRUE if not i check if $new_user == TRUE then I set loggedin = TRUE in my session like this:</p> <pre><code>if(!empty($validate)) { if ($validate == TRUE) { // Log in user $data = array( 'site' =&gt; $site-&gt;site_post, 'id' =&gt;$tenant_id, 'username'=&gt;$username_post, 'user_table'=&gt;$usertable, 'nec_distributor'=&gt;TRUE, 'loggedin' =&gt; TRUE, ); $this-&gt;session-&gt;set_userdata($data); return TRUE; } elseif ($new_user == TRUE) { // Log in user $data = array( 'site' =&gt; $site-&gt;site_post, 'id' =&gt;$new_tenant_id, 'username'=&gt;$username_post, 'user_table'=&gt;$usertable, 'nec_distributor'=&gt;TRUE, 'loggedin' =&gt; TRUE, ); $this-&gt;session-&gt;set_userdata($data); return TRUE; } return FALSE; } </code></pre> <p>Now in my controller i Check like this:</p> <pre><code>$dashboard ='customer/dashboard'; $rules = $this-&gt;distributor_m-&gt;rules; $this-&gt;form_validation-&gt;set_rules($rules); if ($this-&gt;distributor_m-&gt;login() == TRUE) { var_dump($this-&gt;session-&gt;all_userdata()); $this-&gt;distributor_m-&gt;loggedin() == FALSE || redirect($dashboard); redirect($dashboard); } else { $this-&gt;session-&gt;set_flashdata('error', 'That email/password combination does not exist'); //redirect('secure/login', 'refresh'); } </code></pre> <p>But when i submit username and key, all the database transaction are done as per the code successfully. But there is nothing in my session, if I resend the form information then i see the SESSION data. So where I am going wrong?</p>