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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:46:55+00:00 2026-05-21T09:46:55+00:00

I’m a novice with Code Igniter and I’m currently building my own user system.

  • 0

I’m a novice with Code Igniter and I’m currently building my own user system. I’m curretly on the login process and I have implemented a check for whether a user is currently logged in or not.

In my header I then want to display a link to ‘Log Out’ if they are already logged in, or ‘Log In’ if they are not logged in currently.

I have a working function in my index controller as follows, the $loginstatus variable is sent to the my page header view:

function check_session()
{
    //Check session status

            $session = $this->session->userdata('login_state'); 

            $default = "Log In";

            if ($session == 1) 
            {
                $url = site_url('index.php/users/logout'); 
                $status = "Log Out";



            } 
            else 
            {
                $url = site_url('index.php/users/login'); 
                $status = $default;
            }

        $loginstatus = array(
                        "url" => $url,
                        "status" => $status 
                        );

        return $loginstatus;
}

Because it is currently only in the index controller the $loginstatus is not generated for the header view for other pages and this is my problem.

Where would I put this function so that it always loads before my header? I tried creating a libary with a ‘Common’ class and then autoloading that but I ended up with lots of problems.

Thanks in advance.

  • 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-21T09:46:55+00:00Added an answer on May 21, 2026 at 9:46 am

    If you are using CI version bellow 2.0 then create new class in application/libraries/MY_Controller.php, otherwise in application/core/MY_Controller.php and all of your application controllers should extend from it. In this class in the __construct method you will check for the login status and send it to the views.

    class MY_Controller extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
    
            //Get the status which is array
            $login_status = $this->check_session();
    
            //Send it to the views, it will be available everywhere
    
            //The "load" refers to the CI_Loader library and vars is the method from that library.
            //This means that $login_status which you previously set will be available in your views as $loginstatus since the array key below is called loginstatus.
            $this->load->vars(array('loginstatus' => $login_status));
        }
    
        protected function check_session()
        {
            //Here goes your function
        }
    }
    

    Also make sure your application controllers extend from this class

    //application/controllers/index.php
    
    class Index extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
    }
    

    In your views you can do this:
    <a href=”<?php echo $loginstatus[‘url’]; ?>”><?php echo $loginstatus[‘status’]; ?></a>

    This is possible cause the CI_Loader vars() method is doing extract to the parameters passed to it.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.