【问题标题】:redirect function setted to refresh doesn't work correctly设置为刷新的重定向功能无法正常工作
【发布时间】:2012-02-15 09:26:07
【问题描述】:

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

登录控制器

    <?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 */

【问题讨论】:

  • 是否存在可以替代重定向的方式?我在使用 codeigniter 时遇到问题
  • redirect() 函数只不过是 header() php 函数的包装器。是否启用错误报告?在调用重定向之前你有任何输出吗?
  • 是的...启用了错误报告。我有登录页面,当我单击登录按钮时,它不会重定向到主页,而是显示未定义的灰色页面写入未定义。但是如果我使用重定向设置为位置它可以工作(主页正确打开)但是URL 指向登录页面。相反应该指向主页
  • 确切的错误信息是什么?
  • 正如我所说,如果重定向设置为刷新,我会收到一个灰色页面,其中显示单词undefined。

标签: php codeigniter url


【解决方案1】:

您是否尝试过文档中所说的斜线?

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

注意:为了使这个功能起作用,必须在任何东西输出到浏览器之前使用它,因为它利用了服务器头。

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

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

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多