Codeigniter:Controller路由默认控制器Always

时间:2016-02-23 06:49:37

标签: php codeigniter model-view-controller

我将此登录页面作为路由页面中的默认控制器。当用户登录系统时,他们应该选择他们想要的视图。

登录主页

<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>

登录控制器

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());
      }

登录控制器中的每个控制器都能正常工作。 当您第一次加载页面时它会选择控制器,但是当用户注销时系统不再选择系统时,它会加载默认控制器

的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]

因此,当用户退出系统时,应将其带到base_url。 哪个是登录页面。错误是当用户注销并想要登录并选择相同的视图或系统仍然加载base_url()的任何其他视图时。

4 个答案:

答案 0 :(得分:0)

尝试更改您的退出功能

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

答案 1 :(得分:0)

在您的登录控制器中,您应该在重定向功能中使用单引号...例如:

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());
  }

让我知道它是否有帮助......

答案 2 :(得分:0)

登录页面

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

类Login扩展了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');
}

}

/ *文件结束welcome.php / / 位置:./ application / controllers / welcome.php * /

答案 3 :(得分:0)

问题是:在将凭据发送到会话库之前,我将用户重定向到所选控制器。