Welcome Guest, Not a member yet? Register   Sign In
Constructor in controller is behaving strangely
#1

[eluser]brianatlarge[/eluser]
In my controller, I'm checking if a session variable is set. If it is, then I know the user is logged in and I can display the site. If not, then I'll display the login page.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');
        if(!empty($user_email)) {
            $this->index();
        } else {
            $this->login();
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}

The problem with this is it's loading both the home and the login views. What's the deal?
#2

[eluser]nagata[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');

        if(!empty($user_email)) {
        //logged in, do nothing, let the index load safely...
        } else {
           redirect("admin/login");
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}
Its just Index is an initial page loaded if there is no function defined when u are going somewhere
ex: u have admin panel controller,
if u visit admin it will load by default the index()
if there was a function given it will then admin/(function)
...
u see...
it should do anything when user is logged in, if he isnt then just redirect him to login page...
#3

[eluser]Aken[/eluser]
The constructor can do logic, but can't output other controller methods. Use it to check for your login, but not to display views (or load methods that display views).
#4

[eluser]brianatlarge[/eluser]
[quote author="nagata" date="1341513549"]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $user_email = $this->session->userdata('user_email');

        if(!empty($user_email)) {
        //logged in, do nothing, let the index load safely...
        } else {
           redirect("admin/login");
        }
    }

    public function index()
    {
        $this->load->view('admin/home');
    }

    public function login()
    {
       $this->load->view('admin/login');
    }
}
Its just Index is an initial page loaded if there is no function defined when u are going somewhere
ex: u have admin panel controller,
if u visit admin it will load by default the index()
if there was a function given it will then admin/(function)
...
u see...
it should do anything when user is logged in, if he isnt then just redirect him to login page...
[/quote]

Hmm... when I do this, it just loops the redirect part.
#5

[eluser]nagata[/eluser]
Well its cause U should have an seperate controller responding for user authorization...
Like a members controller with reg and login...
and it should redirect to "members/login"...




Theme © iAndrew 2016 - Forum software by © MyBB