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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:49:06+00:00 2026-05-15T17:49:06+00:00

I’m trying to create a universal header for a website built on CodeIgniter, and

  • 0

I’m trying to create a universal header for a website built on CodeIgniter, and I’m having trouble figuring out the code that will switch the ‘Login’ link for the user’s name (with a link to the profile page) after the user logs in.

In the controller functions, I’ve tried the following code:

if(!$this->session->userdata($userSessionVar))
    {
        $data['header_output'] = "<li><a href='" .  base_url() . "index.php/main/login'>Login</a></li>";
    } else 
    {
        $data['header_output'] = $this->session->data('userFirstName');
    }

(I realize this is incomplete, based on my designs, but it’s just to test.) $userSessionVar holds the value “logged in” once logged in. Probably not the best way to do that. And that doesn’t seem to work (and I pass the $data to the view). I’ve also tried making a custom function:

function check_login()
{
$CI =& get_instance();

$userSessionVar = 'logged_in';

if( ! $CI->session->userdata($userSessionVar))
{
    return false;
} return true;
}

And then use the true/false return to structure the $header_output variable. None of these seem to work. I’m new to CodeIgniter and have some intermediate level of PHP/HTML/CSS, etc. I’m sure I’m missing something obvious and would appreciate any help, as well as a heads-up on how to avoid including the code in every controller function.

  • 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-15T17:49:06+00:00Added an answer on May 15, 2026 at 5:49 pm

    The variable $userSessionVar is only available within the function check_login(), so when you try to use it outside of the function, it will be blank (and therefore useless).

    I recommend that you simply use $this->session->userdata('logged_in') and $CI->session->userdata('logged_in') rather than using the variable $userSessionVar to store what appears to be a constant value.

    Also, you have an error in your code. You need to replace $this->session->data('userFirstName') with $this->session->userdata('userFirstName')

    Here’s how I typically deal with user data. First, add auth.php to the models folder:

    <?php
    
    class Auth extends Model {
        private $user_data = false;
        function Auth() {
            parent::Model();
    
            if ($this->input->post('action') == 'login') $this->login();
            else if ($auth_id = $this->session->userdata('auth_id')) {
                $user = // load user data from the database into the variable $user
                if ($user) {
                    $this->user_data = $user;
                } else $this->session->unset_userdata('auth_id');
            }
        }
        function login() {
            // process POST, check with database, and then store user_id using
            // $this->session->set_userdata('auth_id', $user_id_here)
        }
        function me() {
            return $this->user_data? (object)$this->user_data : false;
        }
    }
    
    ?>
    

    Then, auto-load the model. To do this, edit config/autoload.php like so:

    $autoload['model'] = array('auth');
    

    Now your IF statement could look like this:

    if ($me = $this->me()) $data['header_output'] = $me->userFirstName;
    else $data['header_output'] = '<li><a href="'.base_url().'index.php/main/login">Login</a></li>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I know there's a lot of other questions out there that deal with this
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.