Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:25:53+00:00 2026-06-12T19:25:53+00:00

Possible Duplicate: Constructor session validation for different functions Framework : CI (CodeIgniter) Situation :

  • 0

Possible Duplicate:
Constructor session validation for different functions

Framework : CI (CodeIgniter)

Situation :

I have 4 pages(controllers) namely:

home
login
dashboard
editprofile

ACCESS

  • home can be accessed by all types of users, logged in or not

  • login must be only be access if not authenticated

  • dashboard and editprofile must only be accessed by students(authenticated users)

I have this validatation for my controllers like this:

if($this->session->userdata('isLoggedIn')){
    // stay here do the function
} else {
// leave this page
    header('Location:'.base_url().'login');
}

I have that in my function index(){}.

But as I develop the system, as i create more methods, more controllers, it’s becoming messier..
for you need to use this

if($this->session->userdata('isLoggedIn')){
// stay here do the function
} else {
// leave this page
header('Location:'.base_url().'login');
}

everytime you have a method,

i’ve read several questions in stackoverflow… and the only best answer is this: link here

it says that i must use decorator pattern for that… but i don’t clearly get how am i suppose to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:25:54+00:00Added an answer on June 12, 2026 at 7:25 pm

    Create different base controllers for each type of user and then you only have to set that statement once. Our user controller looks like this:

    <?PHP
    class User_Controller extends MY_Controller
    {
    function __construct()
    {
        parent::__construct();      
        if (!$this->session->userdata('is_logged_in')) {
            $this->session->set_flashdata('message', "I'm sorry, but you must  be logged in to view that page.");
            redirect("/");
        }
    }
    }
    

    Then any controllers we want just logged in users to access extend User_Controller automatically making sure any functions are only available if you’re logged in. You need to save this to the core folder to be able to extend it.

    You also need to add this to your config.php to have base controllers with any prefix other than MY_

    function __autoload($class)
    {
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . EXT );
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Constructor vs. Factory in .NET Framework I understand factory patten and have
Possible Duplicate: Interface defining a constructor signature? I have a mixed hierarchy of classes
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: question about copy constructor if I have this snippet of the code
Possible Duplicate: How do you use the non-default constructor for a member? I have
Possible Duplicate: Java - Leaking this in constructor In the NetBeans I have a
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question
Possible Duplicate: Should I initialize variable within constructor or outside constructor I have here
Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.