添加用于重定向的数据数组


Adding A Data Array for redirect

我今天的问题是这个。成功登录后,我想在重定向时将数据数组中的user_id发送到控制面板。这里有很多代码。其中98%的代码是从一个身份验证库中编写的,我担心如果我现在对它进行过多的修改,我最终会因为尝试做一件事而破坏其他东西。任何帮助吗?

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);
    }
}
编辑:

所以我已经包含了模型与控制器,但我得到了这个错误,因为我也包括了模型。

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

模型:

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;    
    }
}           
}

我理解你真正的问题是:

如何获得登录的用户id?

答:

$this->tank_auth->get_user_id()

这将从会话数据中提取user_id,所以你不必每次都发送那个id,你可以"请求"tank_auth库给你:)

PS:如果这不是你想要的,你可能需要考虑换个问题。

后来编辑:

为什么是__construct()?

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

阻止你访问登录之外的任何操作。最确定的是导致了重定向循环