0

My question of the day is this. After a successful login I want to to send the user_id in a data array to the control panel when it redirects. I have a lot of code here. 98% of it was code that was written from a authentication library and I'm afraid if I tinker too much with it now I'll end up breaking something else by trying to do one thing. Any help?

class Auth extends CI_Controller
{
function __construct()
{
    parent::__construct();

    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->load->library('security');
    $this->load->library('tank_auth');
    $this->lang->load('tank_auth');   

    if ($this->tank_auth->is_logged_in()) {
        redirect('/cpanel/');
    } else {
        redirect('/auth/login/');
    }      
}

function index()
{

}

/**
 * Login user on the site
 *
 * @return void
 */
function login()
{
    if ($this->tank_auth->is_logged_in()) {                                 // logged in
        redirect('/cpanel');

    } elseif ($this->tank_auth->is_logged_in(FALSE)) {                      // logged in, not activated
        redirect('/auth/send_again/');

    } else {
        $data['login_by_username'] = ($this->config->item('login_by_username', 'tank_auth') AND
                $this->config->item('use_username', 'tank_auth'));
        $data['login_by_email'] = $this->config->item('login_by_email', 'tank_auth');

        $this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('remember', 'Remember me', 'integer');

        // Get login for counting attempts to login
        if ($this->config->item('login_count_attempts', 'tank_auth') AND
                ($login = $this->input->post('login'))) {
            $login = $this->security->xss_clean($login);
        } else {
            $login = '';
        }

        $data['errors'] = array();

        if ($this->form_validation->run()) {                                // validation ok
            if ($this->tank_auth->login(
                    $this->form_validation->set_value('login'),
                    $this->form_validation->set_value('password'),
                    $this->form_validation->set_value('remember'),
                    $data['login_by_username'],
                    $data['login_by_email'])) {                             // success
                redirect('/cpanel');

            } else {
                $errors = $this->tank_auth->get_error_message();
                if (isset($errors['banned'])) {                             // banned user
                    $this->_show_message($this->lang->line('auth_message_banned').' '.$errors['banned']);

                } elseif (isset($errors['not_activated'])) {                // not activated user
                    redirect('/auth/send_again/');

                } else {                                                    // fail
                    foreach ($errors as $k => $v)   $data['errors'][$k] = $this->lang->line($v);
                }
            }
        }
        $this->template->set_layout('default')->enable_parser(false);
        $this->template->build('auth/login_form', $data);
    }
}

Edit:

So I have included the model with the controller however I'm getting a this error as I'v also included the model.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/cpanel.php

Line Number: 20
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/xtremer/public_html/system/core/Exceptions.php:170)

Filename: core/Common.php

Line Number: 409
A Database Error Occurred

Error Number: 1066

Not unique table/alias: 'users'

SELECT * FROM (`users`, `users`) JOIN `user_profiles` ON `users`.`id` = `user_profiles`.`user_id`

Filename: /home/xtremer/public_html/kowmanager/models/cpanel/dashboard.php

Line Number: 38

Model:

class Dashboard extends CI_Model
{
private $table_name         = 'users';          // user accounts
private $profile_table_name = 'user_profiles';  // user profiles

function __construct()
{
    parent::__construct();

    $ci =& get_instance();
    $this->table_name           = $ci->config->item('db_table_prefix', 'tank_auth').$this->table_name;
    $this->profile_table_name   = $ci->config->item('db_table_prefix', 'tank_auth').$this->profile_table_name;
}

/**
 * Get user info by Id
 *
 * @param   int
 * @param   bool
 * @return  object
 */
function get_user_info($id)
{
    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('user_profiles', 'users.id = user_profiles.user_id');

    $query = $this->db->get($this->table_name);
    if ($query->num_rows() == 1) 
    {
        return $data = $query->row();    
    }
    else 
    {
        return NULL;    
    }

}           

}
2
  • whats the question exactly? I think I am missing it. Commented Aug 19, 2011 at 18:34
  • I have two issues with this. First, after the user logs in it needs to send along the user_id of the person logging in so I can use that variable to post the user's name and other information on the control panel, and second issue is when I logged in yesterday to my script it worked fine but now when i went to it, it gives me a n error messge saying "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." Commented Aug 19, 2011 at 18:40

1 Answer 1

2

From what I undestand your real question is:

How do I get the logged in user id?

Answer:

$this->tank_auth->get_user_id()

This will pull the user_id form the session data, so you don't have to send that id around every time, you can just "ask" the tank_auth library to give it to you :)

PS: If this is not what you want, you might want to consider rephrasing the question.

Later Edit:

Why is this is __construct()?

if ($this->tank_auth->is_logged_in()) { redirect('/cpanel/'); } else { redirect('/auth/login/'); }

That basicaly does, is prevent you form accessing any action in auth other then login. and most surely is causing the redirect loop

Sign up to request clarification or add additional context in comments.

2 Comments

Okay thank you for your response. So I need to take out that who if statement out? And where would I place the $this->tank_auth->get_user_id() function?
after the tank login is approved and in the same function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.