添加数据阵列以进行重定向

时间:2011-08-19 17:46:41

标签: php codeigniter

我今天的问题是这样的。成功登录后,我想在重定向时将数据阵列中的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;    
    }

}           

}

1 个答案:

答案 0 :(得分:2)

从我看来,你真正的问题是:

  

如何获取登录用户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/'); }

那个基本上可以阻止你在auth中访问任何操作,然后登录。并且肯定会导致重定向循环