0
votes

I have this login page as the default controller in the routes page.When users logs in the system they should select the view they want.

login Home Page

<div class="form-group">
    <select class="form-control" name="system" required>
       <option value="">--SYSTEM SELECT--</option>
       <option value="malaria">Malaria</option>
       <option value="familiy">Familiy Planning</option>
       <option value="lab">Laboratory</option>
    </select>
</div>

Login Controller

if($system=="malaria") {
            //redirect to malaria controller
            redirect(base_url().malaria);
      }else if($system=="familiy"){ 
            //redirect t family planning controller     
            redirect(base_url().family);
      }
      else if($system == 'lab'){
            redirect(base_url().lab);
      }else{
           redirect(base_url());
      }

      function logout(){
        $this->session->sess_destroy();
        redirect(base_url());
      }

Each of the controllers in the login controller works perfectly. when you load the page for the first time it selects the controllers, but when the user logs out the system doesn't select the system anymore, it loads the default controller

config.php

 $config['base_url'] = 'http://localhost/trial/';
 $config['index_page'] = '';

routes.php

    $route['default_controller'] = "login";

.htaccess

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

So when the user logs out the system should take them to the base_url. Which is the login page. The error is when the user logs out and wants to log in and selects the same view or any other view the system still loads the base_url().

4
did u load session libaray in this controller??? - devpro
Yes i did in the autoload.php in the config folder - Lmutwiri Daniel
and i am assuming this is not redirecting ?? redirect(base_url());?? - devpro
the base_url working correctly but base_url().* not working - Lmutwiri Daniel
did u load URL helper? for short url???? - devpro

4 Answers

0
votes

Try change your logout function

function logout(){
   $this->session->sess_destroy();
   redirect('login');
}
0
votes

In your login controller you should use single quotes in redirect function ... eg:

if($system=="malaria") {
        //redirect to malaria controller
        redirect(base_url().'malaria');
  }else if($system=="familiy"){ 
        //redirect t family planning controller     
        redirect(base_url().'family');
  }
  else if($system == 'lab'){
        redirect(base_url().'lab');
  }else{
       redirect(base_url());
  }

Let me know if it help or not...

0
votes

Login Page

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *        http://example.com/index.php/welcome
 *    - or -
 *        http://example.com/index.php/welcome/index
 *    - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->view('login');

}


function __encrip_password($password)
{
    return md5($password);
}

/**
 * check the username and the password with the database
 * @return void
 */
function validate()
{
    $this->load->model('user_model');
    $email = $this->input->post('email');
    $password = $this->__encrip_password($this->input->post('password'));
    //Get the value from the form
    $system = $this->input->post('system');

    $is_valid = $this->user_model->validate($email, $password);

    if ($is_valid) {
        //$data['search'] = $system;
        echo "Your Logined In";
        $role = $is_valid[0]['role'];
        $user_id = $is_valid[0]['user_id'];
        $names = $is_valid[0]['names'];
        //print_r($is_valid);
        $data = array(
            'email' => $email,
            'user_id' => $user_id,
            'is_logged_in' => true,
            'role' => $role,
            'names' => $names
        );
         if($system=="malaria") {
            return redirect(base_url().malaria_welcome);
        }
        else if($system=="family"){

           return redirect(base_url().contraceptive_welcome);

        }
        else if($system == 'lab'){

            return redirect(base_url().lab_welcome);
        }else{
            echo "<script>alert('Wrong System')</script>";
            //return redirect(base_url());
        }
        $this->session->set_userdata($data);              
    } else {
       // incorrect username or password
       //echo "Nonesenss";
        redirect(base_url());   
    }
}


function delete_cache(){

}
function logout()
{
    $this->session->sess_destroy();
    redirect('login');
}

}

/* End of file welcome.php / / Location: ./application/controllers/welcome.php */

0
votes

The issue was: I was redirecting the user to the chosen controller before sending the credentials to session library.