Welcome Guest, Not a member yet? Register   Sign In
if session true redirect to dashboard
#1

[eluser]Calebe Aires[/eluser]
How to redirect user for dashboard if he is logged, ( WHEN HE TRIE TO ACCESS THE LOGIN PAGE)?

I have tried this, but it dont work!
Code:
class Entrar extends Controller {
    
    function __construct() {
        parent::Controller();
        $this->load->model('conta_model');

    }

    function index()
    {

        $data['conteudo'] = 'site/entrar';
        $this->load->view('includes/conteudo', $data);
        $this->conectado();  // THIS VERIFY IF USER IS LOGGED, THEN, IF YES, REDIRECT TO DASHBOARD
    

    }

    function conectado(){
        
        $conectado = $this->session->userdata('conectado');
        if(isset($conectado)) {
            
            redirect('/dashboard');


        }else{
            
            die();
                        
        }

    }
    
    function desconectar() {

        $this->session->sess_destroy();
        redirect('/entrar', 'refresh');
        die();

    }

}
#2

[eluser]sketchynix[/eluser]
Make sure the URL helper is loaded

Don't use
Code:
isset(
) when checking the session data. The reason is isset checks to see if the variable exists and that it is not NULL.
Code:
$this->session->userdata()
will either return FALSE or the value of the session data, so the isset will evaluate to true every time.

Let me know if that doesn't work.

Code:
class Entrar extends Controller {
    
    function __construct() {
        parent::Controller();
        $this->load->model('conta_model');
        $this->load->helper('url');
    }

    function index()
    {

        $data['conteudo'] = 'site/entrar';
        $this->load->view('includes/conteudo', $data);
        $this->conectado();  // THIS VERIFY IF USER IS LOGGED, THEN, IF YES, REDIRECT TO DASHBOARD
    

    }

    function conectado(){
        
        $conectado = $this->session->userdata('conectado');
        if($conectado != false) {
            
            redirect('/dashboard');


        }else{
            
            die();
                        
        }

    }
    
    function desconectar() {

        $this->session->sess_destroy();
        redirect('/entrar', 'refresh');
        die();

    }

}




Theme © iAndrew 2016 - Forum software by © MyBB