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

  • Home
  • SEARCH
  • 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 4616962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:03:24+00:00 2026-05-22T02:03:24+00:00

I am fairly new and just started to use Codeigniter, and have come across

  • 0

I am fairly new and just started to use Codeigniter, and have come across some confusion regarding sessions.

What I want to achieve is, like in regular php, I want to check if a user is logged in by using a header include file which checks the session data. I dont want to check/write that checking code in every controller while passing data to the view file.

Can someone please show me how it can be done?

Ex. I don’t want to do the following in every controller:

//Controller:
if($this->session->userdata('loggedin'){
$data['loggedin'] = $this->session->userdata('loggedin');
}
//I dont want to check the above on every function in every controller
$this->load->view('some_view_file', $data);

//some_view_file
if(isset($loggedin)){
echo "You are logged in!";
}
else
{
echo "Please log in!";
}

Instead, I want something to like the following:

//some view file
if(isset($loggedin))
{
echo "your logged in";
}
else
{
echo "please log in";
}

And also, how can I use native php sessions instead of CI Sessions. Any help will be much appreciated. Thanks.

  • 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-22T02:03:25+00:00Added an answer on May 22, 2026 at 2:03 am

    Firstly, theres no reason you can’t just write something like this in your view:

    <? echo ($this->session->userdata('loggedin')) ? "Logged In": "Not Logged In"; ?>
    

    Then your controllers don’t need any of that code.

    However if the check is more complex or something, then theres a few places you can do it.

    1) In the constructor of a custom controller: create a new file application/core/MY_Controller.php, and override the constructor with something like:

    class MY_Controller extends CI_Controller 
    {
        public function __construct() 
        {
            parent::__construct();
            if($this->session->userdata("loggedin")) {
                // do something
            }
        }
    }
    

    then make all your controllers extend MY_Controller.

    2) Or in a post_controller_constructor hook. http://codeigniter.com/user_guide/general/hooks.html (this is more transparent, and probably easier if you have tons of controllers already)

    You can use native sessions with this:
    http://codeigniter.com/wiki/Native_session/

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

Sidebar

Related Questions

I'm fairly new to C and have started out writing a small library with
Being fairly new to JavaScript, I'm unable to discern when to use each of
I am fairly new to Emacs and I have been trying to figure out
I am fairly new to C# language so I just do not understand why
I'm fairly new to Python so hopefully I'm just missing something obvious here, but
I apologize if I'm just missing the obvious. I'm fairly new to Android development,
I'm still fairly new to T-SQL and SQL 2005. I need to import a
I'm fairly new to ASP.NET and trying to learn how things are done. I
I'm fairly new to the STL, so I was wondering whether there are any
I'm fairly new to the world of versioning but would like to introduce Subversion

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.