繁体   English   中英

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

[英]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/'); }

基本上,这是阻止您在auth中访问任何操作,然后登录。 而且最肯定的是导致重定向循环

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM