简体   繁体   English

Codeigniter中的“页面未正确重定向”错误

[英]“The page isn’t redirecting properly” error in codeigniter

i tried to check if user has session in login page, if he has session and role id this controller will redirect to certain controller. 我试图检查用户是否在登录页面中拥有会话,如果他具有会话和角色ID,则此控制器将重定向到某些控制器。 but it gave me error "The page isn't redirecting properly" instead. 但它给了我错误"The page isn't redirecting properly" here's my code 这是我的代码

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Auth extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!$this->session->userdata('username') == null) {
            if ($this->session->userdata('role_id') == 1) {
                redirect(base_url('back/admin'));
            } else {
                redirect(base_url('back/user'));
            }
        } else {
            redirect(base_url('back/auth'));
        }
    }

    public function index()
    {
        $data = ['title' => 'Log-in page'];

        $this->form_validation->set_rules('un', 'Username', 'required|trim');
        $this->form_validation->set_rules('pw', 'Password', 'required|trim');

        if ($this->form_validation->run() == false) {
            $this->load->view('back/login', $data);
        } else { }
    }

    public function logout()
    {
        $this->session->unset_userdata('username');
        $this->session->unset_userdata('role_id');
        $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">you\'re session has been deleted</div>');

        redirect(base_url('back/auth'));
    }

    public function errorp()
    {
        // $this->load->view('back/error');
        echo 'not authorized';
    }
}

i tried many things to solve this redirect error, but still can't find way how to fix this or the way i'm redirect was wrong ? 我尝试了很多方法来解决此重定向错误,但仍然找不到解决该问题的方法或我重定向的方法有误?

certainly, there is an error in logout function. 当然,登出功能有错误。

Try fixing it: 尝试修复它:

(before) (之前)

'<div class="alert alert-success" role="alert">you're session has been deleted</div>'

(after) (后)

'<div class="alert alert-success" role="alert">you\'re session has been deleted</div>'

i got the solution for this error 我为这个错误找到了解决方案

i need to check the session inside my index, no need to check inside __construct, it keep redirecting to the same controller that's why i got that error. 我需要检查索引中的会话,而无需检查__construct,它一直重定向到同一控制器,这就是为什么我得到该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM