0
votes

I have googled for this but did not get anything that worked for my scenario So, I am posting my question. I am working on admin login using CodeIgniter MVC framework, I have written a base controller called 'Admin_Controller' for all my admin controllers which they extended from 'Admin_Controller' I have written login session check in my base controller that is 'Admin_Controller' but it is not working properly saying "localhost redirected you too many times."

Folder structure:

--controllers
   --admin/
     -- Banners.php
     -- Categories.php
     -------
     -------
     -- Login.php
 --models
   -- admin
      -- Mdl_banners.php
      -- Mdl_categories.php
      ------------
      --------------
 --views
   --admin
     -------
     -------

Login Form: admin/login/login_View.php

<form action="<?php echo base_url(); ?>admin/login/validate_user" method="post" name="login_form">
  <h1>Login</h1>
     <?php
        echo "<div class='error_msg'>";
        if (isset($error_message)) {
          echo $error_message;
        }
        echo validation_errors();
        echo "</div>";
     ?>
    <div>
     <input type="text" name="user_name" class="form-control" placeholder="UserName" required="" />
    </div>
    <div>
      <input type="password" name="user_password" class="form-control" placeholder="Password" required="" />
    </div>
    <div>
      <button class="btn btn-default submit" name="submit_login">Login</button>
    </div>
</form>

My Base Controller: core/MY_Controller.php

class MY_Controller extends CI_Controller {
  function __construct()
  {
    parent::__construct();
    // modules::run('admin/login/isUserLoggedin');
  }
}
class Admin_Controller extends MY_Controller {
  public function __construct()
  {
    parent::__construct();
    $this->_title = 'My Site Name';
    // check if user is logged in
    if (! $this->session->userdata('logged_in')) {
      redirect('admin/login');
    }
  }
}

My Login Controller: Controllers/admin/Login.php

class Login extends Admin_Controller {
  public function __construct(){
    parent::__construct();
    $this->load->model('admin/mdl_login');
  }

  public function index(){
    if ($this->session->userdata('logged_in'))
      redirect(base_url('admin/dashboard'));
    $data['title'] = $this->_title . ' - Admin Login';
    $this->load->view('admin/login/login_view');
  }

  public function validate_user(){
    $this->form_validation->set_rules('user_name', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('user_password', 'Password', 'trim|required|xss_clean');

    if ($this->form_validation->run() == FALSE) {
      $this->load->view('login_view');
    } else {
      $data = array(
        'username' => $this->input->post('user_name'),
        'password' => md5($this->input->post('user_password'))
      );
      $result = $this->mdl_login->login($data);
      if ($result == TRUE) {
        $user_name = $this->input->post('user_name');
        $result = $this->mdl_login->read_user_information($user_name);
        if ($result != false) {
          $session_data = array(
            'user_id' => $result[0]->id,
            'user_name' => $result[0]->username,
            'email' => $result[0]->email,
          );
          // Add user data in session
          $this->session->set_userdata('logged_in', $session_data);
          redirect('admin/dashboard');
        }
      } else {
        $data = array(
          'error_message' => 'Invalid Username or Password'
        );
        $this->load->view('admin/login/login_view', $data);
      }
    }
  }
}

htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

My problem is It is showing as "localhost redirected you too many times." My doubt is that is there any problem to have 'Login' controller inside 'admin' folder in 'controllers'? Anyone point me in right direction? Thanks.

1
That happens because of loop, please reformat your code so it's worth reading and digging. Where you show the login form? and the form would submit to validate_user - Adi Prasetyo
I have updated my question with login form, please check it once @Adi Prasetyo - Prasad Patel
aside from this q, you should use PHP password-hashing API instead of md5. Do you able to see the form before redirect loop? - Adi Prasetyo
No I am unable to see the login form, If I access http://localhost/site_name/admin/login then it is showing the error as localhost redirected you too many times. - Prasad Patel
instead of redirect try to load view - jobayersozib

1 Answers

0
votes

redirect('admin/login') will look for controller named Admin and method login. you can change to 'foo/bar' where foo is your desired controller and bar is the method.

given this function

public function bar() {
  echo "I am about to show the login form here";
}

You can utilize remap or route.

to break the redirect loop firstly understand the concept (use remap or routing) then you should modify your code to execute the above function.