title: how to use session in login system and implement logout in codeigniter
body: <p>New in codeigniter learning it and stuck on the session for codeigniter.
  I am working on a login system the login part is working good but now i have to add session and logout and include my other pages in to that session so i can restrict someone to direct access that page. so how i implement session &amp; logout in my code. </p>
  
  <p>This is the Controller</p>
  
  <pre><code>&lt;?php
  class LoginController extends CI_controller
          {
              public function index()
          {
              $this-&gt;load-&gt;view('header2');
              $this-&gt;load-&gt;view('login');
              $this-&gt;load-&gt;view('footer');
          }
              public function checklogin()
          {
              $this-&gt;form_validation-&gt;set_rules('username' ,'Username', 'required|valid_email');
              $this-&gt;form_validation-&gt;set_rules('password' ,'Password', 'required|callback_verifyUser');
  
                  if($this-&gt;form_validation-&gt;run() == false){
                      $this-&gt;load-&gt;view('header2');
                      $this-&gt;load-&gt;view('login');
                      $this-&gt;load-&gt;view('footer');
                      }
                      else
                      {
                          redirect('HomeController/index');
                      }
                  }
                      public function verifyUser()
                      {
                          $name = $this-&gt;input-&gt;post('username');
                          $pass = $this-&gt;input-&gt;post('password');
  
                          $this-&gt;load-&gt;model('LoginModel');
  
                          if($this-&gt;LoginModel-&gt;login($name, $pass))
                          {
                              return true;
                          }
                          else
                          {
                              $this-&gt;form_validation-&gt;set_message('verifyUser','Incorrect Email or Pass');
                              return false;
                          }
                          redirect('LoginController/checklogin');
                      }
              }
  </code></pre>
  
  <p>The model is like this</p>
  
  <pre><code>&lt;?php
          class LoginModel extends CI_model
          {
              public function login($name, $pass)
              {
                  $this-&gt;db-&gt;select('name,pass');
                  $this-&gt;db-&gt;from('members');
                  $this-&gt;db-&gt;where('name',$name);
                  $this-&gt;db-&gt;where('pass',$pass);     
  
                  $query = $this-&gt;db-&gt;get();
  
                  if($query-&gt;num_rows() == 1)
                  {
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }
          }
  </code></pre>
  
  <p>The View is </p>
  
  <pre><code>                  &lt;html&gt;
                      &lt;head&gt;
                      &lt;title&gt;&lt;/title&gt;
                  &lt;/head&gt;
                  &lt;body&gt;
                  &lt;?php echo validation_errors(); ?&gt;
                  &lt;?php echo form_open('LoginController/checklogin'); ?&gt;
                  UserName: 
                  &lt;input type="text" name="username" /&gt;&lt;br/&gt;
                  password:
                  &lt;input type="text" name="password" /&gt;
                  &lt;input type="submit" value="Login" name="submit" /&gt;
                  &lt;/form&gt;
                  &lt;/body&gt;
                  &lt;/html&gt;
  </code></pre>
  
  <p>login is working correctly need a session and logout in this.</p>