views:

47

answers:

1

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  A: 

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>';
Mark Eirich
Thanks for the quick response. I'm trying to get my head around it -- will $user be reset every time the user visits a new page, or only once and it will persist throughout the user's session? And what are the advantages of this as opposed to loading all the user data into a session?
tchaymore
@tchaymore: Yes, I reload the user data from the database on every page load. This is usually trivial when compared to all the other database queries you'll be running. The main advantage is that if the user's information/permissions (which may be set by other users) change between requests, $this->auth->me() will always return fresh data. Any data that you store in the session can get stale. Also, it's a good idea to keep the session data small, especially if the data is stored in a cookie. On small sites this argument may not be as persuasive, but in any case, it makes the code simpler.
Mark Eirich
@Mark Eirich: Thanks for the explanation. However, the code gives me an "Parse error: syntax error, unexpected T_PRIVATE" for the "private $user_data = false;" line.
tchaymore
@tchaymore: Are you using PHP4? If so, replace "private" with "var" like this: `var $user_data = false;` Otherwise, I don't know why you'd get that error, unless you left out the curly brace on the line before or something.
Mark Eirich