在POST方法上加载视图或重定向

时间:2015-12-23 05:22:33

标签: php codeigniter

我正在尝试在POST方法上加载视图或重定向。但我无法做到。

if($_POST){

        $this->load->model('Staff_model');

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if(!empty($username) && !empty($password)){
            $result = $this->Staff_model->getUserByUsernameAndPassword($username, $password);

            if($result){
                $sess_array = array();
                $sess_array = array(
                        'id' => $result['id'],
                        'username' => $result['username'],
                        'name' => $result['name'],
                        'status' => $result['status']                   
                );

                $this->session->set_userdata('logged_in', $sess_array);                 
                redirect('dashboard', 'refresh');
            }else{
                $this->load->view('login_view');
            }
        }

在上面的代码中,我需要在$ result变为false的情况下加载'login_view'视图。或者我需要重定向到仪表板控制器。但在任何一种情况下,我都会得到空白页。

这是完整的代码。

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

class Login extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        if($_POST){

            $this->load->model('Staff_model');

            $username = $this->input->post('username');
            $password = $this->input->post('password');

            if(!empty($username) && !empty($password)){
                $result = $this->Staff_model->getUserByUsernameAndPassword($username, $password);

                if($result){
                    $sess_array = array();
                    $sess_array = array(
                            'id' => $result['id'],
                            'username' => $result['username'],
                            'name' => $result['name'],
                            'status' => $result['status']                   
                    );

                    $this->session->set_userdata('logged_in', $sess_array);                 
                    redirect('dashboard', 'refresh');
                }else{
                    $this->load->view('login_view');
                }
            }


        }else{
            $this->load->view('login_view');
        }
    }
}

1 个答案:

答案 0 :(得分:1)

U发送类似的值

$this->load->view('login_view',$sess_array);

并在视图页面中回显您想要的值。

echo $sess_array('username');