PHP/codeigniter - use of exit()

前端 未结 8 981
不思量自难忘°
不思量自难忘° 2020-12-28 16:46

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

$this->checkSession();
//...rest of the code
相关标签:
8条回答
  • 2020-12-28 17:01
    $this->output->_display();
    exit(); 
    

    Is the correct answer! Thanks to Sam Sehnert... It's hidden in the comments so thought I'd re-post.

    0 讨论(0)
  • 2020-12-28 17:02

    I had a similar problem. Where I wanted to stop the user to due to no login. But I wanted to offer a list of links for them not simply redirect them to a login page. I am using CI version 1.7.2 and the $this->_output() $this->display->_output() and $this->output->_display() solutions did not work for me. I was however to get my results using the $this->output->get_output() function.

            $this->load->vars($data);
            $this->load->view('template');
            die($this->output->get_output());
    
    0 讨论(0)
  • 2020-12-28 17:02

    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.

    0 讨论(0)
  • 2020-12-28 17:02

    Actually in the newest CI function to manually call output class is

    $this->display->_output();
    

    and don't be worried - it handles caching, content will also be properly gzipped if you set so in config

    I usually add and extended controller with login logic that handles login functions, so that if a normal controller is one that is needing an auth then the login method is called automatically and the original content is not displayed. It's a good solution if you would like to stay on the page the user tried to access without redirecting (and then posting him back to the same page)

    0 讨论(0)
  • 2020-12-28 17:05

    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.

    0 讨论(0)
  • 2020-12-28 17:08

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题