CodeIgniter: view does not load if I use die () function

I have the following code. Checks if the user is logged in or not. When $ is_logged_in is not set or False, I load the message view. Unfortunately, at the same time, the system loads limited content. So I used the die () function, and now only a blank page is displayed.

What can I do to download only the message view when the user is not logged in? Thanks.

if(!isset($is_logged_in) OR $is_logged_in == FALSE) { $data['main_content'] = 'not_logged_in'; $data['data'] = ''; $this->load->view('includes/template',$data); die(); } 
+4
source share
4 answers

Anyway. I used redirection to login page and flashdata variable p>

 if(!isset($is_logged_in) OR $is_logged_in == FALSE) { $this->session->set_flashdata('error_msg','You must be logged in to access restricted area'); redirect('login/'); } 

thanks

+3
source

In fact, I found the answer to the mantain URL and do not redirect:

 $data['main_content'] = 'unauthorized_access'; $this->load->view('includes/template', $data); // Force the CI engine to render the content generated until now $this->CI =& get_instance(); $this->CI->output->_display(); die(); 
+19
source

I talked with this for a while. If you use die or exit after trying to load the view, CI displays a blank page.

The solution would be to use return , which stops the execution of the current function and does nothing after that.

A simple example:

 public function validate(){ //validation code if(!$valid){ $this->load->view('error'); return; } //This code won't run } 
+1
source

CI probably uses output buffering (see http://www.php.net/manual/en/ref.outcontrol.php ). If you want to load the view and kill the script, you will need to flush the buffer. This is usually done at the very end of the script, but die () IN stops it from there.

 if(!isset($is_logged_in) OR $is_logged_in == FALSE) { $data['main_content'] = 'not_logged_in'; $data['data'] = ''; $this->load->view('includes/template',$data); ob_flush(); die(); } 
0
source

All Articles