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

时间:2011-08-17 21:38:29

标签: 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);
    }
}

编辑:

当我进入我的kansasoutlawwrestling.com/kowmanager页面时,使用以下代码我得到一个白色的屏幕,但如果我去登录页面是kansasoutlawwrestling.com/kowmanager/login并登录然后我得到一个PHP错误被遇到

严重性:注意

消息:未定义的变量:id

文件名:controllers / auth.php

行号:63 遇到PHP错误

严重性:警告

消息:无法修改标头信息 - 已经发送的标头(输出从/home/xtremer/public_html/system/core/Exceptions.php:170开始)

文件名:helpers / url_helper.php

行号: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);
    }
}

1 个答案:

答案 0 :(得分:1)

实际上@Pekka,tank_auth使用语言库,所以句子不存在,但是对句子的英文版本的引用。您要查找的行就在文件$data['login_by_email'])) { // success

中的controllers/auth.php之后

它使用auth控制器内部的_show_message()函数。该函数在闪存会话数据中设置消息,然后将您重定向到主auth视图(显示存储在闪存中的消息)。您可以使用简单的redirect('myControlPanel');替换此行并且您会没事的。或者做我做的事情并将它们返回到将它们重定向到登录页面的页面。

希望有所帮助, 最大