1

我尝试将 CodeIgniter 与 vagrant (使用 puphpet 创建的机器)一起使用。

ip 地址是192.168.56.101,当我尝试访问主页时,我得到一个循环重定向到/index.php/login

此代码在尝试从 VM 内部访问它时有效,但在主机浏览器中我得到一个循环......

这是响应的标头:

Refresh:0;url=http://192.168.56.101/index.php/login

还有一些配置设置:

// application/config/config.php
$config['base_url'] = 'http://192.168.56.101/';
$config['index_page'] = 'index.php';

任何想法 ?如果需要,我可以发布更多代码。谢谢

编辑:根据要求,这里有更多信息:

//index.php

define('ENVIRONMENT', 'development');                                                                                                                                                                                                                            
if (defined('ENVIRONMENT'))                                                                                                                                                                                                        
{                                                                                                                                                                                                                                  
  switch (ENVIRONMENT)                                                                                                                                                                                                             
  {                                                                                                                                                                                                                                
    case 'development':                                                                                                                                                                                                            
      error_reporting(E_ALL);                                                                                                                                                                                                      
    break;                                                                                                                                                                                                                         
    case 'testing':                                                                                                                                                                                                                
    case 'production':                                                                                                                                                                                                             
      error_reporting(0);                                                                                                                                                                                                          
    break;                                                                                                                                                                                                                      
    default:                                                                                                                                                                                                                       
      exit('The application environment is not set correctly.');                                                                                                                                                                   
  }                                                                                                                                                                                                                                
}                                                                                                                                                                                                                         
  $system_path = 'system';                                                                                                                                                                                               
  $application_folder = 'application';                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  if (defined('STDIN'))                                                                                                                                                                                                            
  {                                                                                                                                                                                                                                
    chdir(dirname(__FILE__));                                                                                                                                                                                                      
  }                                                                                                                                                                                                                                 
  if (realpath($system_path) !== FALSE)                                                                                                                                                                                            
  {                                                                                                                                                                                                                                
    $system_path = realpath($system_path).'/';                                                                                                                                                                                     
  }                                                                                                                                                                                                                                                                                                                                                                                                                            
  $system_path = rtrim($system_path, '/').'/';                                                                                                                                                                                           
  if ( ! is_dir($system_path)){                                                                                                                                                                                                                                
    exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));                                                                  
  }                                                                                                                                                                                                       
  define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));                                                                                                                                                                           
  define('EXT', '.php');                                                                                                                                                                                                           
  define('BASEPATH', str_replace("\\", "/", $system_path));                                                                                                                                                                        
  define('FCPATH', str_replace(SELF, '', __FILE__));                                                                                                                                                                               
  define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));                                                                                                                                                                  
  if (is_dir($application_folder))                                                                                                                                                                                                 
  {                                                                                                                                                                                                                                
    define('APPPATH', $application_folder.'/');                                                                                                                                                                                    
  }                                                                                                                                                                                                                                
  else                                                                                                                                                                                                                             
  {                                                                                                                                                                                                                                
    if ( ! is_dir(BASEPATH.$application_folder.'/'))                                                                                                                                                                                                                                                                                                                                                                      
  if (is_dir($application_folder))                                                                                                                                                                                                 
  {                                                                                                                                                                                                                                
    define('APPPATH', $application_folder.'/');                                                                                                                                                                                    
  }                                                                                                                                                                                                                                
  else                                                                                                                                                                                                                             
  {                                                                                                                                                                                                                                
    if ( ! is_dir(BASEPATH.$application_folder.'/'))                                                                                                                                                                               
    {                                                                                                                                                                                                                              
      exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);                                                                                            
    }                                                                                                                                                                                                                              

    define('APPPATH', BASEPATH.$application_folder.'/');                                                                                                                                                                           
  }                                                                                                                                                                                                                                

require_once BASEPATH.'core/CodeIgniter.php';   

主控制器:

// application/core/MY_Controller.php

class MY_Controller extends CI_Controller                                                                                                                                                                                          
{                                                                                                                                                                                                                                  
    public function __construct()                                                                                                                                                                                                  
    {                                                                                                                                                                                                                              
        parent::__construct();                                                                                                                                                                                                     
        if ($this->session->userdata('username') === false)                                                                                                                                                                        
        {                                                                                                                                                                                                                          
            redirect('login', 'refresh');                                                                                                                                                                                          
        }                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                              
} 

登录控制器:

// application/controller/login.php

class Login extends CI_Controller                                                                                                                                                                                                  
{                                                                                                                                                                                                                                  

  public function index()                                                                                                                                                                                                          
  {                                                                                                                                                                                                                                
      $username = $this->session->userdata('username');                                                                                                                                                                            
      if ($username === false) {                                                                                                                                                                                                   
        $view_data = array();                                                                                                                                                                                                      
        $view_data['alert'] = $this->session->flashdata('alert');                                                                                                                                                                  
        $this->load->view('login',$view_data);                                                                                                                                                                                     
      }                                                                                                                                                                                                                            
      else {                                                                                                                                                                                                                       
        redirect('home', 'refresh');                                                                                                                                                                                               
      }                                                                                                                                                                                                                            
  }                                                                                                                                                                                  
  public function connect() {                                                                                                                                                                                                                                                                                                                                                                                                                       
    $this->load->model('user_model');
    $username = $this->session->userdata('username');                                                                                                                                                                              
    if ($username === false) {                                                                                                                                                                                                     
      $post_username = $this->input->post('username');                                                                                                                                                                             
      $post_password = $this->input->post('password');                                                                                                                                                                                                                              
      $data_bdd = $this->user_model->get_user($post_username);                                                                                                                                                                     
      $this->session->set_flashdata('alert', 'user unknown');                                                                                                                                                    
      foreach ($data_bdd as $user) {                                                                                                                                                                                               
        $this->session->flashdata('alert');                                                                                                                                                                                        
        if ($this->encrypt->decode($user->USER_PASSWORD) == $post_password) {                                                                                                                                                      
          $this->session->set_userdata('username',$post_username);                                                                                                                                                                 
        }                                                                                                                                                                                                                          
        else{                                                                                                                                                                                                                      
          $this->session->set_flashdata('alert', 'incorrect password');                                                                                                                                                
        }                                                                                                                                                                                                                          
      }                                                                                                                                                                                                                            
    }                                                                                                                                                                                                                              
    redirect('login', 'refresh');                                                                                                                                                                                                  
  }                                                                                                                                                                                                                                

  public function disconnect() {                                                                                                                                                                                                   
    $this->session->unset_userdata('username');                                                                                                                                                                                    
    redirect('login', 'refresh');                                                                                                                                                                                                  
  }
}
4

1 回答 1

0

我通过删除以下行找到了解决方案:

// application/core/MY_Controller.php

class MY_Controller extends CI_Controller                                                                                                                                                                                          
{                                                                                                                                                                                                                                  
    public function __construct()                                                                                                                                                                                                  
    {                                                                                                                                                                                                                              
        parent::__construct();                                                                                                                                                                                                     
        //if ($this->session->userdata('username') === false)                                                                                                                                                                        
        //{                                                                                                                                                                                                                          
        //    redirect('login', 'refresh');                                                                                                                                                                                          
        //}                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                              
} 

但我无法再访问任何网站功能(因为我没有登录)。我仍然无法访问登录页面...

于 2014-11-03T00:21:41.570 回答