设置为刷新的重定向功能无法正常工作

时间:2012-02-15 09:26:08

标签: php codeigniter url

我正在构建一个jquery移动站点并使用codeigniter。我构建了一个登录和主页面。我使用了redirect函数但是设置它来刷新,不允许正确加载主页面。设置为'location'正确加载主页面,但即使我正确登录并显示主页面,URL仍然是登录页面的URL。我应该在URL中看到主页面控制器

enter image description here

登录控制器

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

/**
 * Description of login_form
 *
 * @author apocalipse89
 */
class Login extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('User','user'); /* This call the model to retrieve data from db */
    }

    public function index()
    {
        if(!file_exists('application/views/_login.php'))
        {
            show_404();
        }

        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');

        $this->form_validation->set_rules('username','username','trim|required|xss_clean');
        $this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');

        if($this->form_validation->run() == FALSE)
        {
            /* Data to pass to view */
            $data['title'] = "User Access";
            $data['author'] = "Salvatore Mazzarino";
            $data['year'] = date('Y');

            $this->load->view('templates/_header',$data);
            $this->load->view('_login',$data);
           /* $this->load->view('templates/_footer',$data);*/
        }
        else 
        {   
            redirect('home','refresh');
        }
    }

    public function pass_check($pass)
    {        
        $result = $this->user->find_user($this->input->post('username'),$pass);

        if(!empty($result))
        {            
                $session_array = array('id'=> $result->id, 'username'=> $result->username); /* Create a session passing user data */

                $this->session->set_userdata('logged_in', $session_array);

                return TRUE;
        }
        else
        {
            $this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
            return FALSE;
        }
    }
}

/* END OF FILE */

家庭控制器

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Description of home
 *
 * @author apocalipse89
 */
class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {        
        if($this->session->userdata('logged_in'))
        {                        
            $data['title'] = "Management Emergency";
            $data['author'] = "Salvatore Mazzarino";
            $data['year'] = date('Y');

            $this->load->view('templates/_header', $data);
            $this->load->view('_home',$data);
            $this->load->view('templates/_footer',$data);
        }
        else
        {
            redirect('home');
        }
    }

    public function logout()
    {
        $this->session->unset_userdata('logged_in');
        redirect('home');
    }
}

/* END OF FILE */

1 个答案:

答案 0 :(得分:1)

你是否按照文档说的那样尝试使用斜杠?

redirect('/home/','refresh');

注意:为了使此功能起作用,必须在将任何内容输出到浏览器之前使用它,因为它使用服务器头。

注意:要对标题进行非常精细的控制,您应该使用输出库的set_header()函数。

文档:http://codeigniter.com/user_guide/helpers/url_helper.html