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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:11:08+00:00 2026-06-15T09:11:08+00:00

I have Ion Auth properly installed and working on my server. I also have

  • 0

I have Ion Auth properly installed and working on my server. I also have the default CodeIgniter 2 “news” tutorial working in the same CI installation. I’m just playing around and curious about the proper way to use the authentication system to “enclose” or protect an entire application.

For this question, let’s use the “news” tutorial that comes with CI.

Inside the index() function in my news.php controller, I added conditional code to check if the user is logged in. If not, the user is just taken to the login screen.

public function index() {
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';
    if ($this->ion_auth->logged_in()) {
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    } else {
        redirect('auth/login', 'refresh');
    }
}

I can see this works, but the immediate downside is that every function within the controller would also have to be modified with similar conditional logic to protect all other page views. e.g. – check for login, display page, else go to login page… over and over.

Is this the way it’s supposed to be done?

What if an application is already built and working and one simply wants to protect it all? Adding conditional logic to check login status on every single page view within the controller seems unnecessarily verbose.

Can the whole application (all views) be protected in one place to minimize code modification? If so, how?

  • 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-15T09:11:12+00:00Added an answer on June 15, 2026 at 9:11 am

    To protect an entire controller, you can put the auth check into the __construct() call as eric.itzhak mentioned.

    To protect an entire application, you can extend the CI_Controller class, put the auth in the constructor of that file, and then finally extend by MY_Controller instead of CI_Controller in each of your controllers.

    Code examples:

    /* File: application/core/MY_Controller.php */
    class MY_Controller extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
    
            if ( ! $this->ion_auth->logged_in())
            {
                redirect('auth/login');
            }
        }
    }
    

    And then, in each controller (note MY_Controller, not CI_Controller):

    class Controller_name extends MY_Controller
    {
        function __construct()
        {
            parent::__construct();
        }
    
        // rest of controller methods
    }
    

    These code examples assume you’re autoloading (you might as well) the ion auth library. If not, load the library in the MY_Controller file as necessary.

    There are two advantages to this method:

    1. You only have to change the CI_Controller to MY_Controller in each controller you want to protect.
    2. You don’t have to protect everything which is helpful if you need to have an unprotected controller, i.e. the controller containing the auth methods (you won’t be able to login if your auth controller requires you to be logged in 😛 — there will be a redirect loop).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Codeigniter and Ion Auth installed on MAMP. I have the libraries working
I have authentication up and running in CodeIgnitor using Ion Auth, seems to be
I use the ion auth for codeigniter and it works great except that I
Have deployed numerous report parts which reference the same view however one of them
I'm using Codeigniter/PyroCMS which uses ion_auth. I have my session stuff set for two
I have a web application built with jQuery Mobile and PHP (CodeIgniter framework). Now
I have just started using ionauth , and I am trying to implement the
I have a website built on Codeigniter with Ion_auth for authentication. I'm now building
I have been working on speeding up a query I'm using for about a
Hi team i am new to CI and ion auth and i want to

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.