codeigniter没有在另一个控制器中检索会话

时间:2016-07-01 15:59:04

标签: php codeigniter session

我完全被困在这里,我创建了一个控制器登录,它获得了,用DB设置规则和认证,并在代码结束时设置了会话。 此外,我还有另一个显示仪表板的控制器仪表板,还必须使用会话来查看是否设置了会话,如果真用户可以使用控制器,则重定向到登录页面。那就是它 但我无法在仪表板中进行会话,我该怎么办?

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

class Login extends CI_Controller
{

    /**
     * Login constructor.
     */
    public function __construct()
    {
        parent::__construct();

        //Load Form helper
        $this->load->helper('form');

        //Load form validation library
        $this->load->library('form_validation');
    }

    /**
     *Display Login page
     */
    public function index()
    {

        $title['title'] = 'Admin login';
        $this->load->view('login', $title);
    }


    /**
     *
     * Set rules for authentication
     *
     * @return bool
     */
    public function rules()
    {

        $rules = array(
            array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'remember',
                'label' => 'Remember',
                'rules' => 'trim|int|xss_clean'
            )
        );

        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run() != FALSE) {
            $this->authentication();
        } else {

            $title['title'] = 'Admin login';
            $this->load->view('login', $title);
        }
    }

    /**
     * Check if user is exist
     */
    public function authentication()
    {

        $data = array(
            'username' => $this->input->post('username', true),
            'password' => $this->input->post('password', true)
        );

        $this->load->model('userData');
        if ($this->userData->getUserData($data) == FALSE) {

            $title['title'] = 'Admin login';
            $this->load->view('login', $title);
        } else {

            $this->set_session($data);
            redirect('dashboard/index');
        }

    }

    /**
     * @param $data username|password
     */
    public function set_session($data)
    {

        $session_data = array(
            'username' => $data['username'],
            'login' => true
        );

        $this->session->set_userdata('auth', $session_data);
    }

}

这是仪表板:

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


class Dashboard extends CI_Controller{

    public function __construct()
    {
        parent::__construct();

    }

    /**
     * Display dashboard 
     */
    public function index(){


        $array = $this->session->userdata('auth');
        $login = $array['login'];

        if ($login == true) {
            $title['title'] = 'Dashboard';
            $this->template->load('panel/dashboard', $title);
        }else{
            redirect('login/index');
        }     
    }
}

1 个答案:

答案 0 :(得分:0)

你做错了。您无法访问函数外部的$_post值。你也不需要authentication()功能。

当你调用这个函数if ($this->form_validation->run() != FALSE)并且它的PASS,然后在这里添加``authentication()`函数代码。然后你可以设置会话。

代码示例

$this->form_validation->set_rules($rules);

if ($this->form_validation->run() != FALSE) {
    $data = array(
        'username' => $this->input->post('username', true),
        'password' => $this->input->post('password', true)
    );

    $this->load->model('userData');
    if ($this->userData->getUserData($data) == FALSE) { # Uncler this point

        $title['title'] = 'Admin login';
        $this->load->view('login', $title);
    } else {

        $this->set_session($data);
        redirect('dashboard/index');
    }
} else {

    $title['title'] = 'Admin login';
    $this->load->view('login', $title);
}