Welcome Guest, Not a member yet? Register   Sign In
Login redirect error
#1

[eluser]jcjc[/eluser]
I've created a MY_Controller to house an isLoggedIn function which will check if the session data has been set before allowing them into a page. Problem I have is when I go to view another page that's protected it reloads the dashboardView. I need it check if user is logged in before showing the page else send them to login.

Code:
class MY_Controller extends CI_Controller
{

    function __construct()
    {
      
        parent::__construct();

        $this->is_logged_in();

    }

    function isLoggedIn()
    {
     $is_logged_in = $this->session->userdata('is_logged_in');

     if(!isset($is_logged_in) || $is_logged_in != TRUE)
     {
      redirect('/login');
     }
     else
     {
//THIS LINE I THINK NEEDS TO BE DYNAMIC.
      $this->load->view('dashboardView');
     }
    }


    

}


Thanks
#2

[eluser]ivantcholakov[/eluser]
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->_is_logged_in();
    }

    protected function _is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');

        if (empty($is_logged_in))
        {
            // This is for normal HTTP requests.
            // You may check here and add behavior for AJAX too.
            redirect('login');
        }

        // THIS LINE I THINK NEEDS TO BE DYNAMIC.
        // Re: Just allow to continue, the class that extends MY_controller
        // will decide what view to load.
    }

}
#3

[eluser]ivantcholakov[/eluser]
I your site has pages that are publicly accessed and pages accessed by logged users only, I think you need to create yet another base controller, sort of MY_Public_Controller.
#4

[eluser]jcjc[/eluser]
No all pages are private.
#5

[eluser]jcjc[/eluser]
also tried your amendments and it doesn't work.

#6

[eluser]jcjc[/eluser]
Sorry - scratch that,

added this instead of empty() and it's working - thanks.


Code:
if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
     {
      redirect('login');
     }




Theme © iAndrew 2016 - Forum software by © MyBB