views:

44

answers:

2

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

What can I do to only load 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();
}
A: 

CI is probably using output buffering (see http://www.php.net/manual/en/ref.outcontrol.php). If you want to load a view and kill the script, you will need to flush the buffer. This would normally be done at the very end of the script, but die()ing stops it from getting 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();
}
Ash White
When I put ob_flush() throws:A PHP Error was encounteredSeverity: NoticeMessage: ob_flush() [ref.outcontrol]: failed to flush buffer. No buffer to flush.Thanks
T.C
+1  A: 

Anyway. I used a redirect to the login page, and a flashdata variable

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

T.C