1

I'm basically trying a session control. If the user is logged in, it's ok to move on. But if he's not logged in, then it will show a log-in screen and then die. However, when I use die or exit in the constructor, it does not show the log-in screen; it immediately dies. The code is as following:

private $username = null;
private $mongoid = null;
private $neoid = null;

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

    // session to global
    $this->username = $this->session->userdata( 'username');
    $this->mongoid = $this->session->userdata( 'mongoid');
    $this->neoid = $this->session->userdata( 'neoid');

    // check if user is logged in
    if( $this->username == "" || empty( $this->username)){
        $this->load->view( 'access/login');
        die;
    }
}

It shows the log-in page if die is not written there, but with die, it does not show. Why do I want to use die? Because if I don't use, it moves on the index function and I don't want it to execute index function if the user is not logged in.

What is wrong here? What should I use to stop executing?

12
  • You need to show it to user using echo or something first. Commented Oct 27, 2012 at 17:00
  • 2
    what happens if you replace die; with: echo $this->output->get_output(); exit; Commented Oct 27, 2012 at 17:02
  • 3
    why are you building this yourself? Does CI not have an Authentication Bundle or something? In any case, you dont want to have that check in the User class, let alone in the constructor. It does not belong there but in an Authentication Service class. Commented Oct 27, 2012 at 17:03
  • Show what? The view page? Or anything? Commented Oct 27, 2012 at 17:03
  • John, thank you. It is solved. Can you please post it an answer, so I can mark as the right answer? Commented Oct 27, 2012 at 17:05

3 Answers 3

1

CodeIgniter does not instantly output the view when you load it with $this->load->view();, it puts the output of the view to the output buffer instead and after everything is loaded it flushes the whole output to the user. With that said, the problem you are seeing is that it buffers the output, then it dies without flushing it.

die is really bad and should not be used outside debugging, you should better use something like a variable switch. If it's only for the controllers scope, then you can make a

private $show_everything_else = true;

In the constructor:

if( $this->username == "" || empty( $this->username)){
    $this->load->view( 'access/login');
    $this->show_everything_else = false;
}

In any of the controller's methods:

if($this->show_everything_else) {
    // ...
}

In any case, this solution is a quick fix and there are much better possibilities how to do this, but all of them require a different approach.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, it's a good approach but I'd rather not using if-else statements constantly.
Then better make a library and move all the logic there, or at least a model.
1

You can have a method and call it in constructor:

function __construct() {
    parent::__construct();
    $this->_is_logged_in();
}

and the method should look like this:

function _is_logged_in() {
    $is_logged_in = $this->session->userdata('is_logged_in');
    if (!isset($is_logged_in) || $is_logged_in != true) {
        redirect('login');
        die();
    }
}

And, of course, you should have controller for login, which can look like this:

<?php

class Login extends CI_Controller {

    function index() {
        $this->load->view('LOGIN-VIEW');
    }

    function validate() {
        $this->load->model('login_model');
        $data = $this->login_model->validate();

        if ($data != false) {
            $data['is_logged_in'] = true;
            $this->session->set_userdata($data);
            redirect('MAIN-CONTROLLER-AFTER-LOGIN');
        }
        else {
            $this->index();
        }
    }

    function logout() {
        $this->session->sess_destroy();
        $this->index();
    }

}

This what i posted, also preserve sessions in database.

Login model can be as primitive as this:

class Login_model extends CI_Model {

function validate() {


    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('users');

    if($query->num_rows == 1) {
        $data = $query->row_array();
             return $data;
        } else {
            return false;
        }
}

Comments

1

The Controller class will automatically run the index() function. So, to make it work, you must return the index() if the login check in construct is failed. Here what you can do:

private $username = null;
private $mongoid = null;
private $neoid = null;
private $no_login = false;

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

    // session to global
    $this->username = $this->session->userdata( 'username');
    $this->mongoid = $this->session->userdata( 'mongoid');
    $this->neoid = $this->session->userdata( 'neoid');

    // check if user is logged in
    if( $this->username == "" || empty( $this->username)){
        $this->load->view( 'access/login');
        $this->no_login = true;
    }
}

function index(){
    if($this->no_login) return;
    // another statement if login success.....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.