【问题标题】:Codeigniter 3 redirect issueCodeigniter 3重定向问题
【发布时间】:2017-01-26 18:22:01
【问题描述】:

我正在尝试使用 Codeigniter 3 中的以下代码将未登录的用户重定向到登录页面:

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

codeigniter 安装在名为“admin”的子文件夹中。

我有控制器 Auth.php 并且方法 login 存在,但仍然无法正常工作,我收到的唯一信息是 404 Not found 错误

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';

还有身份验证控制器:

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');
        }
    }

}

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

【问题讨论】:

  • 它是否正确地将您重定向到/auth/login
  • 重定向的 url 是 /auth/login,我得到一个 404 错误页面..
  • 所以,即使您手动访问site.com/auth/login 也不起作用,这是正确的吗?您通常如何登录? (网址)
  • 到目前为止我只有一个想法,$config['base_url'] = 'localhost/admin' 并且在路由中我有 $route['admin'] = 'admin/dashboard';不知何故有一个错误..
  • 是的,即使手动访问也会生成 404 页面..

标签: php codeigniter


【解决方案1】:

试试

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

而不是重定向。并且不要忘记在 codeigniter 配置中指定基本 url

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

【讨论】:

  • 请为您的答案添加一些进一步的解释 - 为什么这应该是更好的方法?
猜你喜欢
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2016-07-02
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多