Pass session between controller

2019-07-18 02:19发布

问题:

Can you explain me a method to pass session data between controllers? in CodeIgniter?

'Cause if I have

$data = array('email' => $this->input->post('email'),
                                    'is_logged_in' => true);
        $this->session->set_userdata($data);

On Pippo.php controller I cant' use the session in another controller, I need to check if 'is_logged_in' is set to true to create member only pages.

回答1:

First I would autoload the session library.

Then in the success part of your login controller

$data = array(
      'email' => $this->input->post('email'),
      'is_logged_in' => true
);

$this->session->set_userdata($data);

Then once you have set the sessions and have been redirect to your controller

In your parent::__construct() area of a controller.

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

  if ($this->session->userdata('is_logged_in') == false) {
    redirect('logout_controller');
  }
}