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:

With the following code when I go to my kansasoutlawwrestling.com/kowmanager page I get a white screen but if I go my login page which is kansasoutlawwrestling.com/kowmanager/login and log in then I get A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/auth.php

Line Number: 63 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: helpers/url_helper.php

Line Number: 543

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');   

    $id = $this->tank_auth->get_user_id();

}

function index()
{

}

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

    } 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', $id);

            } 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);
    }
}
4
  • 2
    Remove the welcome message in the code? Commented Aug 17, 2011 at 21:43
  • Well after I log in I get this: Hi, [my screen name here]! You are logged in now. With a logout link. Instead of it showing that I want it to go to the admin controller I'm not seeing where it's telling it to load that welcome message Commented Aug 17, 2011 at 21:48
  • 1
    One idea that comes to mind is searching for the sentence in all the files in your project - it's often the fastest way Commented Aug 17, 2011 at 21:50
  • Good point I should do that first. Commented Aug 17, 2011 at 21:54

1 Answer 1

1

Well actually @Pekka, tank_auth uses language libraries, so the sentence isn't there, but the reference to the english version of the sentence. The line you're looking for is right after $data['login_by_email'])) { // success in the file controllers/auth.php

It uses the _show_message() function inside of the auth controller. The function sets a message in the flash session data, then redirects you to the main auth view (which displays the message stored in flash). You can replace this line with a simple redirect('myControlPanel'); And you'll be fine. Or do what I did and return them to the page that redirected them to the login page.

Hope that helps, Max

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

12 Comments

Sorry I can't, if you'd like, we could start a chat room if you need further assistance with this question. Or you could post up the next bit of the problem you are having.
load the tank auth library and call: $id = $this->tank_auth->get_user_id();, there is also $this->tank_auth->get_username() and $this->tank_auth->is_logged_in();.
Place what? If you mean the library load ($this->load->library('tank_auth');) You can put that anywhere before you call a function for it (I usually do it at the top of the page though). You can call the get_user_id() anywhere though, just assign it to a variable, and you have the current logged in user's id.
After you load the tank_auth library, and the parent::__construct(); yes
|

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.