Adding A Data Array for redirect
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开发者_C百科.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);
}
}
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
精彩评论