views:

334

answers:

4

I have a few pages that require login, so all controllers that link to these pages start with

$this->checkSession();
//...rest of the code

CheckSession should verify the session is still live, otherwise display a message and stop the execution of the rest of the code in the controller:

function checkSession()
{
    if (!$this->session->userdata('is_logged_in'))
    {
        //the session has expired!
        $data['main'] = 'confirmation_message';
        $data['title'] = "Session expired";
        $this->load->vars($data);
        $this->load->view('template');
        exit();
    }
}

. I was expecting these instructions to happen in sequence, but I only get a blank page. How can I make sure exit() gets executed only after all views are loaded?

A: 

I don't know enough about codeigniter's workflow but it seems to me that you want to redirect to the login page instead of trying to render it. Evidently, none of the code you supplied sends the template to the browser by the time exit() is called.

Mike B
+1  A: 

exit() cuts your scrip there and the actual _output() function of the controller is never called. What you need to do is add action in one of your controllers for example the user login screen and redirect there. You can use the flashdata function from the Session - http://codeigniter.com/user_guide/libraries/sessions.html to pass your message and then catch it inside your view and display it.

Another way which is not very smart but should work is to forcefully call the output function.

function checkSession()
{
    if (!$this->session->userdata('is_logged_in'))
    {
        //the session has expired!
        $data['main'] = 'confirmation_message';
        $data['title'] = "Session expired";
        $this->load->vars($data);
        $this->load->view('template');
        $this->_output();
        exit();
    }
}
Ivo Sabev
+2  A: 

Hi,

In this case you should not use exit, what you should do if the session is not valid is redirect your app using example:

redirect('/init/login/','refresh');

Best Regards,
Pedro

Pedro
+3  A: 

In this case Pedro is correct. If they are not logged in just redirect them, it's even better if you can use Public/Admin named base controllers to stop you having to do this in each separate protected file.

Generally speaking though, if you use exit() it will stop the Output library for running. If you just want to stop the current controller from executing but allow output of the controller you can use return in exactly the same way.

function checkSession()
{
    return (bool) $this->session->userdata('is_logged_in');
}

Then simply:

if(!$this->checkSession())
{
        //the session has expired!
        $data['main'] = 'confirmation_message';
        $data['title'] = "Session expired";
        $this->load->vars($data);
        $this->load->view('template');
        return;
}

exit() should only ever be used if you really want instant death of your application's execution for debugging, error reporting, etc.

Phil Sturgeon
seems a good solution, although I'm still a beginner - I'll keep it mind for when I have a better grasp of CI.
Patrick
Great answer. First time I ever realise the difference between exit() and return.
JannieT