loliki
loliki

Reputation: 957

Codeigniter show controller if session exists

I'm new to codeigniter and learn it.

I got 2 controllers, 1st one called main and responds for user registration / login the second one is called todo and shows a todo list.

Now if I access localhost/list my website opens the page, where do I write the session logic to test if user is logged in?

My controller todo

defined('BASEPATH') OR exit('No direct script access allowed');

class Lists extends CI_Controller {

public function index()
{
    $this->load->view('lists');}
}

How do I display it using the session:

if($this->session->userdata('is_logged_in') == 1)

Or do I have to put the session logic before each function?

Upvotes: 0

Views: 861

Answers (1)

Razib Al Mamun
Razib Al Mamun

Reputation: 2713

You put the session $this->session->userdata('is_logged_in') condition in __construct()

Like this :

<?php
class Lists extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if($this->session->userdata('is_logged_in') != 1) { 
            //redirect code here
        }
    }

    public function index() {
        $this->load->view('lists');}
    }
}

Upvotes: 1

Related Questions