简体   繁体   中英

“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. but it gave me error "The page isn't redirecting properly" instead. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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