rosuandreimihai
rosuandreimihai

Reputation: 656

Codeigniter 3 redirect issue

I'm trying to redirect a not logged in user to a login page, using this code inside Codeigniter 3:

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

The codeigniter is installed on a subfolder called "admin".

I have the controller Auth.php and the method login exists, but still it doesn't work, the only thing I receive is a 404 Not found error

The routs.php file contains:

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

And the auth controller :

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

}

Could someone point me to the correct way of redirecting to a specific page?

Upvotes: 1

Views: 1074

Answers (1)

Mumin Gazi
Mumin Gazi

Reputation: 38

Try

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

Instead of redirect. And don't forget to specify base url in codeigniter config

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

Upvotes: 1

Related Questions