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

The Archive Base Latest Questions

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

I am attempting to create a constructor for my controller that references a function

  • 0

I am attempting to create a constructor for my controller that references a function that I have contained in a helper which is autoloaded.

The function checks whether or not the user is logged in, if so it redirects them to the login page.

It appears that I have not setup the construct correctly as I am receiving the following error:

Fatal error: Call to undefined method Profile::is_logged_in()

This is the controller:

<?php

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

class Profile extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            //function inside autoloaded helper, check if user is logged in, if not redirects to login page
            $this->is_logged_in();
       }

    public function index() {

    echo 'hello';

    }

} 

I only want to make function within the controller accessible if the user is logged in.

This is the helper which is autoloaded

$autoload['helper'] = array('url','array','html','breadcrumb','form','function','accesscontrol');

(accesscontrol_helper.php):

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

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
            die();      
            //$this->load->view('login_form');
        }       
    }

Why would I not be able to run the function? Is containing the code in the helper the best method?

  • 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:47:10+00:00Added an answer on May 27, 2026 at 1:47 am

    As other already mentioned, helpers are simply a collection of functions. Expanding on them:

    • since they’re loaded more than once sometimes, you need to specify not to declare a function if already present, all you’ll raise an error.
    • You cannot, moreover, call a CI’s class inside them without first instantiating the main CI object. This is a more proper way to use your helper function:

      if(!function_exists('is_logged_in'))    
      {
          function is_logged_in()
          {
          $CI =& get_instance();
          $is_logged_in = $CI->session->userdata('is_logged_in');
             if(!isset($is_logged_in) || $is_logged_in != true)
             {
              echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
              die();      
             }       
          }
      }
      

    I would also have it return instead of echo, and move the die() to the controller, but this is another story.

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

Sidebar

Related Questions

I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I am attempting to create a web page that will allow a user to
I'm attempting to create a simple WPF UserControl beginning with the default constructor. public
I am attempting to create a type synonym that looks something like this: data
I have been attempting to develop an embedded webserver within an application that I
I'm attempting to create an xquery expression that will return selected nodes but will
I am attempting to create a profile page that shows the amount of dwarves
I am attempting to create an overloaded operator for a matrix class that I
I'm currently attempting to create a tabbed interface in a web application, and based

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.