【问题标题】:Is there a way to redirect to a specific page using Tank_auth in Codeigniter?有没有办法使用 Codeigniter 中的 Tank_auth 重定向到特定页面?
【发布时间】:2013-08-21 06:41:32
【问题描述】:

我想知道是否有办法重定向到 Tank_auth 中的特定页面(如 adminpanel_view)。 我查看了 Auth.php 控制器,但不知道如何重定向,如果可能的话..

我试过这个:

(Auth.php)

public function login() //login functie
{
    $this->breadcrumbs->page = array('link'=> base_url().'auth/login' ,'title' => 'Login');         
    $data['breadcrumbs'] = $this->breadcrumbs->get();
    if ($this->tank_auth->is_logged_in()) {                                 // logged in
        redirect('members/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['use_recaptcha'] = $this->config->item('use_recaptcha', 'tank_auth');
        if ($this->tank_auth->is_max_login_attempts_exceeded($login)) {
            if ($data['use_recaptcha'])
                $this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
            else
                $this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
        }
        $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('');

            } 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);
                }
            }
        }
        $data['show_captcha'] = FALSE;
        if ($this->tank_auth->is_max_login_attempts_exceeded($login)) {
            $data['show_captcha'] = TRUE;
            if ($data['use_recaptcha']) {
                $data['recaptcha_html'] = $this->_create_recaptcha();
            } else {
                $data['captcha_html'] = $this->_create_captcha();
            }
        }
        $this->load->view('views/header');
        $this->load->view('auth/login_form', $data);
        $this->load->view('views/footer');
    }
}

【问题讨论】:

    标签: php codeigniter redirect login tankauth


    【解决方案1】:

    确保在使用函数base_url、site_url 或redirect 之前已经加载了url_helper。并且您的代码无法运行,因为您错过了一个大括号。应该是

    public function login() //login functie
    {
        $this->load->helper('url');
        $this->breadcrumbs->page = array('link'=> site_url('auth/login') ,'title' => 'Login');         
        $data['breadcrumbs'] = $this->breadcrumbs->get();
        if ($this->tank_auth->is_logged_in()) {      // logged in
            redirect('members/cpanel');
        }
    }
    

    【讨论】:

    • 实际上这不是全部代码。我没有忘记添加一个大括号{ 我已经加载了 url_helper。这与我尝试过的完全相同。
    • 那么,重定向时发生了什么?
    • 没什么。默认情况下,它仍会重定向到主页。
    • 为什么应该有另一个重定向?我会发布...我会发布我的整个功能。
    • 我的意思是 members/cpanel 不是 login 函数
    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 2011-05-11
    • 2014-04-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    相关资源
    最近更新 更多