如何在codeigniter中提交表单后解决“302 found”响应

时间:2012-11-26 05:34:56

标签: php codeigniter http-status-code-302

我正在使用codeigniter,我有一个登录视图,我将表单提交给控制器以获取登录功能,但我得到的响应是302 Found,并重定向到我的登录页面。

我的控制器代码是

class super_admin extends CI_Controller
{

    public function index()
    {
        $this->load->view('admin/login_view');
    }

    function checkLogin()
    {
        $email = mysql_real_escape_string($_POST['email']);
        $pwd = mysql_real_escape_string($_POST['pwd']);

        $this->load->model('admin/admin_model');
        $user = $this->admin_model->checkUser($email);
        //print_r($user[0]);
        if ($user && ($user[0]->password == md5($pwd)))
        {

            $user_data = array(
                            'user_id' => $user[0]->user_id,
                            'user_name' => $user[0]->name,
                            'user_email' => $user[0]->email
                        );

            $this->session->set_userdata($user_data);
            $this->load->view('admin/dashboard_view',$user_data);
        }
        else
        {
            $this->session->set_flashdata('invalid', '<div class="alert alert-error" id="invalid" ><h6>Invalid Email or Password</h6></div>');
            redirect('admin/super_admin');
        }
    }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请不要在您提交表单的方法中加载视图使用重定向,否则在刷新页面时您没有收到帖子请求,并且您的条件返回false会导致故障。试试这个解决方法。还要检查你的$ user变量是什么意思。

class super_admin extends CI_Controller
{

    public function index()
    {
        $this->load->view('admin/login_view');
    }

    function checkLogin()
    {
        $email = $this->input->post('email', TRUE);
        $pwd = $this->input->post('pwd', TRUE);

        $this->load->model('admin/admin_model');
        $user = $this->admin_model->checkUser($email);
        //print_r($user[0]);
        if ($user && ($user[0]->password == md5($pwd)))
        {

            $user_data = array(
                            'user_id' => $user[0]->user_id,
                            'user_name' => $user[0]->name,
                            'user_email' => $user[0]->email
                        );

            $this->session->set_userdata($user_data);
            redirect('super_admin/dashboard');
        }
        else
        {
            $this->session->set_flashdata('invalid', '<div class="alert alert-error" id="invalid" ><h6>Invalid Email or Password</h6></div>');
            redirect('admin/super_admin');
        }
    }

    function dashboard()
    {
       if( $this->session->userdata('user_id') ) {
         $user_data = $this->session->all_userdata();
         $this->load->view('admin/dashboard_view',$user_data);
       } else {
         redirect('admin/super_admin');
       }
    }
}