2014-10-31 131 views
0

我嘗試使用CodeIgniter與vagrant(用puphpet創建的機器)。CodeIgniter Vagrant重定向循環

IP地址爲192.168.56.101,當我嘗試訪問的主網頁,我得到一個環路重定向到/index.php/login

試圖從虛擬機內訪問它時,此代碼工作,但主機瀏覽器中我得到環......

這裏是從響應的頭:

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');                                                 
    } 
} 
+0

什麼是您的默認控制器?如果用戶未登錄,是否將其重定向到登錄控制器?讓我知道它的索引函數。 – 2014-10-31 12:37:19

+0

@ShaifulIslam:更新,你怎麼看? – nobe4 2014-11-02 18:21:52

+0

我認爲它總是顯示你登錄form.may是這行永遠不會執行$ this-> session-> set_userdata('username',$ post_username); – 2014-11-02 18:53:33

回答

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');                                               
     //}                                                       
    }                                                        
} 

但我不再訪問任何網站功能(因爲我沒有登錄)。我仍然無法訪問登錄頁面...