使用社区auth codeigniter登录后重定向

时间:2017-10-25 20:10:04

标签: php codeigniter authentication redirect

我无法理解如何将用户重定向到登录后他所在的上一个网址。

如果用户没有查看权限,我在控制器中有以下代码(所以我将其重定向到登录页面):

//User is anonymous -> go to login page (and remember the current URL for redirecting again after log in)
$this->session->set_flashdata('referred_from', current_url());
redirect(site_url( 'login?redirect=user', $redirect_protocol));

我在Auth控制器中的登录功能如下所示:

public function login()
    {
        // Method should not be directly accessible
        if ($this->uri->uri_string() == 'user/login/login')
            show_404();

        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post')
            $this->require_min_level(1);

        $this->setup_login_form();

        // Passing Variables
        $data['title'] = lang('page_title_login');
        $data['class'] = 'login';
        $data['description'] = lang('meta_desc_login');
        $current = $this->namemanager->getBreadName("login");
        $data['bread'] = $this->breadcrumbmanager->getBreadcrumb($current);
        $data['loadGoogleApi'] = true;
        $data['loadFacebook'] = true;

        $data['referred_from'] = $this->session->flashdata('referred_from');

        // Template declaration
        $partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation', 'content' => 'user/login_form', 'footer' => '_master/footer/footer');
        $this->template->load('_master/master', $partials, $data);
    }

现在,当我在referenced_from上执行var_dump时,它确实包含正确的值。

但是,我的登录视图包含以下表单开放代码:

echo form_open( $login_url, ['class' => 'std-form'] );

$ login_url显然是在Auth_Controller.php文件中设置的,是:

protected function setup_login_form( $optional_login = FALSE )
    {
        $this->tokens->name = 'login_token';

        /**
         * Check if IP, username, or email address on hold.
         *
         * If a malicious form post set the on_hold authentication class 
         * member to TRUE, there'd be no reason to continue. Keep in mind that 
         * since an IP address may legitimately change, we shouldn't do anything 
         * drastic unless this happens more than an acceptable amount of times.
         * See the 'deny_access_at' config setting in config/authentication.php
         */
        if( $this->authentication->on_hold === TRUE )
        {
            $view_data['on_hold_message'] = 1;
        }

        // This check for on hold is for normal login attempts
        else if( $on_hold = $this->authentication->current_hold_status() )
        {
            $view_data['on_hold_message'] = 1;
        }

        // Display a login error message if there was a form post
        if( $this->authentication->login_error === TRUE )
        {
            // Display a failed login attempt message
            $view_data['login_error_mesg'] = 1;
        }

        // Redirect to specified page
        /* Original Redirect
        $redirect = $this->input->get('redirect')
            ? '?redirect=' . $this->input->get('redirect')
            : '?redirect=' . config_item('default_login_redirect');
        */

        $redirect = $this->input->get('redirect')
            ? '?redirect=' . config_item('default_login_redirect')
            : '?redirect=' . config_item('default_login_redirect');

        // If optional login, redirect to optional login's page
        if( $optional_login )
        {
            $redirect = '?redirect=' . urlencode( $this->uri->uri_string() );

            $view_data['optional_login'] = TRUE;
        }

        // Set the link protocol
        $link_protocol = USE_SSL ? 'https' : NULL;

        // Load URL helper for site_url function
        $this->load->helper('url');

        // Set the login URL
        $view_data['login_url'] = site_url( LOGIN_PAGE . $redirect, $link_protocol );

        $this->load->vars( $view_data );
    }

当我进入浏览器并单击登录按钮时,URL会自动显示如下:

http://localhost/codeigniter/login?redirect=user

为了重定向到referenced_from变量,我需要做什么(如果填写了这个变量)?

1 个答案:

答案 0 :(得分:0)

将视图更改为

if (!empty($referred_from)) {
        echo form_open( $login_url . $referred_from, ['class' => 'std-form'] );
    } else {
        echo form_open( $login_url, ['class' => 'std-form'] );
    }

做了伎俩!