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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:22:44+00:00 2026-06-11T04:22:44+00:00

I want to check if user is logged in CodeIgniter by using my library

  • 0

I want to check if user is logged in CodeIgniter by using my library in the controller’s constructor.

This is my library:

class Administrator_libs {

    public function validate_authen(){
        if( $this->session->userdata('user_authen') ){
            redirect(base_url().'admin/login/');
        }   
    }
}

And this is my controller:

class Administrator extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->library('administrator_libs');
        $this->administrator_libs->validate_authen();
        $this->load->model('mod_menu');
    }
}

But I get this error message:

Undefined property: Administrator_libs::$session

How can I use session in a library in CodeIgniter?

  • 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-11T04:22:45+00:00Added an answer on June 11, 2026 at 4:22 am

    If you want to access any CodeIgniter library inside of your own, you must call get_instance(). This is because $this is bound to your current library and not the CodeIgniter object.

    $CI =& get_instance();
    if( $CI->session->userdata('user_authen') ){
        redirect(base_url().'admin/login/');
    }
    

    Please see Creating Libraries CodeIgniter Documentation. Specifically the content under Utilizing CodeIgniter Resources within Your Library

    This assumes you autoload the session library in config/autoload.php, if not, you’ll also need to add $CI->load->library("session"); after $CI instantiation.

    IMPORTANT: =& is not a typo. It’s passed by reference to save memory.

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

Sidebar

Related Questions

I made a login button and want to check if the user logged in
i want code to check whether the user in logged in or not. I
In a Java Servlet I want to check programmatically whether a user is logged
I'm using a filter in web.xml to check if a user is logged in
Using WordPress - I want to check who has just logged in and if
I want check whether a logged in user has permissions like write or Read
I m using Ajax call to check whether a user has logged in or
In php I want to check if the user is logged in. Usualy it
On my page i want to check if the user is logged in and
I want to check for user inactivity in my application. I did a little

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.