title: Check login and redirect in Codeigniter not working?
body: <p>I have a problem when use <strong>check_login</strong> function.<br>
  When I enter <a href="http://localhost/shopping/admin/login" rel="nofollow">http://localhost/shopping/admin/login</a> and put admin username and password, it does not redirect to <strong>home admin page</strong>. But when I try to
  <a href="http://localhost/shopping/admin" rel="nofollow">http://localhost/shopping/admin</a>. It still redirect to <strong>login page</strong>.</p>
  
  <pre><code>/**
   * MY_Controller constructor.
   */
  class MY_Controller extends CI_Controller
  {
      public $data = array();
      function __construct()
      {
          parent::__construct();
          //  $this-&gt;load-&gt;library('session');
  
          $controller = $this-&gt;uri-&gt;segment(1);
          switch ($controller) {
              case 'admin': {
                  $this-&gt;load-&gt;helper('admin');
                  $this-&gt;_check_login();
                  break;
              }
              default: {
  
              }
          }
      }
  
      /*
       * Check login admin
       */
      function _check_login()
      {
          $controller = $this-&gt;uri-&gt;rsegment('1');
          $controller = strtolower($controller);
          $login = $this-&gt;session-&gt;userdata('login');
          //redirect to login if not logged in.
          if (!$login &amp;&amp; $controller != 'login') {
              redirect(admin_url('login'));
          }
          //redirect to home if login successful.
          if ($login &amp;&amp; $controller == 'login') {
              redirect(admin_url('home'));
          }
      }
  }
  
  /*
   * admin_helper
   */
  function admin_url($url = '')
  {
      return base_url('admin/'.$url);
  }
  </code></pre>
  
  <p>But when I remove</p>
  
  <pre><code>//redirect to login if not logged in.
  if (!$login &amp;&amp; $controller != 'login') {
      redirect(admin_url('login'));
  }
  </code></pre>
  
  <blockquote>
    <p>After I login with my admin, it redirect to home page successful.</p>
    
    <p>Why is it not working?</p>
  </blockquote>
  
  <pre><code> /*
    * Controller login
    */
  class Login extends MY_Controller
  {
  
      function index()
      {
  
          $this-&gt;load-&gt;library('form_validation');
          $this-&gt;load-&gt;helper('form');
  
          if ($this-&gt;input-&gt;post()) {
              $this-&gt;form_validation-&gt;set_rules('login', 'login', 'callback_check_login');
              if ($this-&gt;form_validation-&gt;run()) {
                  $this-&gt;session-&gt;set_userdata('login', true);
  
                  redirect(admin_url('home'));
              }
          }
          $this-&gt;load-&gt;view('admin/login/index');
      }
  
      /*
       * Kiem tra login
       * */
      function check_login()
      {
          /*
          $this-&gt;load-&gt;library('form_validation');
          $this-&gt;load-&gt;helper('form');
          */
          $username = $this-&gt;input-&gt;post('username');
          $password = $this-&gt;input-&gt;post('password');
          $password = md5($password);
  
          $this-&gt;load-&gt;model('admin_model');
          $where = array('username' =&gt; $username, 'password' =&gt; $password);
          if ($this-&gt;admin_model-&gt;check_exists($where)) {
              return TRUE;
          }
          $this-&gt;form_validation-&gt;set_message(__FUNCTION__, 'Login fail.');
          return FALSE;
  
  
      }
  }
  </code></pre>