Codeigniter 3重定向问题

时间:2017-01-26 18:22:02

标签: php codeigniter

我尝试将未登录的用户重定向到登录页面,使用Codeigniter 3中的此代码:

if ( !$this->aauth->is_loggedin() OR !$this->aauth->is_allowed("dashboard"))
{
    redirect('auth/login', 'refresh');
}
else
{ //show stuff }

codeigniter安装在名为" admin"。

的子文件夹中

我有控制器Auth.php并且方法登录存在,但它仍然无法正常工作,我收到的唯一一件事是404找不到错误

routs.php文件包含:

$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['admin'] = 'admin/dashboard';
$route['admin/prefs/interfaces/(:any)'] = 'admin/prefs/interfaces/$1';

和auth控制器:

class Auth extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library("Aauth");

        $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

        $this->lang->load('auth', 'romanian');


    }


    function index()
    {
        if ( ! $this->aauth->is_loggedin() )
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
            redirect('/', 'refresh');
        }       
    }


    function login()
    { 
        $this->data['success'] = false;
        if ($this->input->is_ajax_request()) {  
           if ($this->aauth->login($this->input->post('identity'), $this->input->post('password'))) {
               $this->data['success'] = true;
           } else {
               $this->data['message'] = 'Utilizator sau parola gresite!';
           } 
           echo json_encode($this->data);
           die();  
        }
        if ( ! $this->aauth->is_loggedin())
        {
            /* Load */
            $this->load->config('admin/dp_config');
            $this->load->config('common/dp_config');

            /* Valid form */
            $this->form_validation->set_rules('identity', 'Identity', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('remember_me', 'Remember Me', 'optional');

            /* Data */
            $this->data['admin_assets']        = $this->config->item('admin_assets');
            $this->data['title']               = $this->config->item('title');
            $this->data['title_lg']            = $this->config->item('title_lg');
            $this->data['auth_social_network'] = $this->config->item('auth_social_network');
            $this->data['forgot_password']     = $this->config->item('forgot_password');
            $this->data['new_membership']      = $this->config->item('new_membership');

            if ($this->form_validation->run() == TRUE)
            {
                $remember = (bool) $this->input->post('remember');

                //if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
                if ($this->aauth->login($this->input->post('identity'), $this->input->post('password'), $remember)) 
                {
                    //if ( ! $this->ion_auth->is_admin())
                    if ( $this->aauth->is_allowed("dashboard"))
                    {       
                        $this->session->set_flashdata('message', $this->ion_auth->messages());    
                        redirect('/', 'refresh');
                    }
                    else
                    {
                        /* Data */
                        $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

                        /* Load Template */
                        $this->template->auth_render('auth/choice', $this->data);
                    }
                }
                else
                {
                    $this->session->set_flashdata('message', $this->ion_auth->errors());
                    redirect('auth/login', 'refresh');
                }
            }
            else
            {
                $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

                $this->data['identity'] = array(
                    'name'        => 'identity',
                    'id'          => 'identity',
                    'type'        => 'email',
                    'value'       => $this->form_validation->set_value('identity'),
                    'class'       => 'form-control',
                    'placeholder' => lang('auth_your_email')
                );
                $this->data['password'] = array(
                    'name'        => 'password',
                    'id'          => 'password',
                    'type'        => 'password',
                    'class'       => 'form-control',
                    'placeholder' => lang('auth_your_password')
                );

                /* Load Template */
                $this->template->auth_render('auth/login', $this->data);
            }
        }
        else
        {      
            redirect('/', 'refresh');
        } 
   }


    function logout($src = NULL)
    {
        //$logout = $this->ion_auth->logout();
        $logout = $this->aauth->logout();

        //$this->session->set_flashdata('message', $this->ion_auth->messages());

        if ($src == 'admin')
        {
            redirect('auth/login', 'refresh');
        }
        else
        {
            redirect('/', 'refresh');
        }
    }

}

有人能指出我重定向到特定页面的正确方法吗?

1 个答案:

答案 0 :(得分:0)

尝试

$link = base_url()."auth/login";
header('Location: '.$link);

代替重定向。并且不要忘记在codeigniter配置中指定基本URL

$config['base_url'] = "http://YOU-DOMAIN.com/YOUR-SUBDOMAIN/";