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 6855513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:05+00:00 2026-05-27T01:46:05+00:00

Let say at my controller named Book , I have many methods, such as

  • 0

Let say at my controller named Book, I have many methods, such as
get_book(); read_book(); remove_book();

No methods in the class can be used without user logged in, and I can get the user_id from session.

My question is, what is/are the best ways to check the if the user_id session is set so that I can use the methods?

As for now I am thinking of creating a is_logged_in() method, and apply it to every methods with an if-else statement, like

if($this->is_logged_in()
{
   //do something
}
else
{
   //redirect to home
}  

Isn’t it long and tedious? Is there an ultimate way to achieve this?

I read the link

codeigniter check for user session in every controller

But it seems that I still have to apply the is_logged_in check at every methods.

Thank you for helping me!

  • 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-05-27T01:46:06+00:00Added an answer on May 27, 2026 at 1:46 am

    Create a file called MY_controller.php (the prefix can be edited in config file) in /application/core:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Controller extends CI_Controller {
    
    
        function __construct()
        {
    
            parent::__construct();
    
            //Initialization code that affects all controllers
        }
    
    }
    
    
    class Public_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
    
            //Initialization code that affects Public controllers. Probably not much needed because everyone can access public.
        }
    
    }
    
    class Admin_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
            //Initialization code that affects Admin controllers I.E. redirect and die if not logged in or not an admin
        }
    
    }
    
    class Member_Controller extends MY_Controller {
    
        function __construct()
        {
            parent::__construct();
    
            //Initialization code that affects Member controllers. I.E. redirect and die if not logged in
        }
    
    }
    

    Then anytime you create a new controller, you decide what access it requires

        class Book extends Member_Controller {
    
            //Code that will be executed, no need to check anywhere if the user is logged in. 
            //The user is guaranteed to be logged in if we are executing code here.
    
    //If you define a __construct() here, remember to call parent::__construct();
        }
    

    This cuts code duplication a lot, since if you need another member controller other than Book you can just extend the Member_Controller. Instead of having to do the checks in all of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a method at Controller named book($chapter,$page); where $chapter and $page
Let say I am at my View loaded by my controller named Book. Is
So let's say we have a domain object such as the following public class
Let's say we have a first Object 'Controller' and it initializes another object named
Can rails handle creating a view without a controller? For example, let say I
Let say I have abstract class called: Tenant and Customer. The tenant in this
Let's say I have the following rule routes.MapRoute( Default, // Route name {controller}/{action}/{id}, //
I have a problem with passing values from model to controller, let's say i
Let's say the controller name is TemplateUserController whose model class is TemplateUser . Now,
Let's say my main controller 'hotels' has a pattern for the url, such as:

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.