Welcome Guest, Not a member yet? Register   Sign In
Login Box and Logged In User data on each page.
#4

[eluser]thewindsurfer[/eluser]
So I have a solution to this. But I want to check with the experts here to see if my logic is sound and if there is a better more efficient way to do this.

I created a MY_Controller and put it into my application/core

Here is its code

Code:
class MY_Controller extends CI_Controller {
    
    function __construct(){
        parent::__construct();
        $this->is_logged_in();
    }
    //Check if this user is logged in.
    function is_logged_in(){
        $is_logged_in = $this->session->userdata('is_logged_in');
        
        if(!isset($is_logged_in) || $is_logged_in != true){
            $data['is_logged_in'] = FALSE;
        }else{
            $data['is_logged_in'] = TRUE;
        }
        
        //Set a global var so this can be used in views.
        $this->load->vars($data);
        
        //Set a return so this can be used as a function calls in controllers.
        return $data['is_logged_in'];
    }

}

Basically the function checks for the "is_logged_in" session and then creates a variable in the $data array called "is_logged_in" and sets it to either true or false.

It then sets that variable to global. So the views can use it without me having to set it in individual controllers.

I also put a return on this so that I can call this function inside individual controllers to see if a user is logged in. (is there a better way to do this part?)

I then created a mini-view called loginbox.php which I call where I need my loginbox to be display (currently my header).

Here is the code.

Code:
<?php
    if($is_logged_in == TRUE){
        echo "You Are Logged in!";
        echo anchor('user/logout','Logout');
    }
    else{
        echo form_open('user/login_validate');
        echo form_input('username','Username');
        echo form_password('password','Password');
        echo form_submit('submit','Login');
        echo anchor('user/signup','Create Account');
        echo '<br/>';
        if($this->session->flashdata('login_error')){
            echo "You Entered Someting Wrong!";
        }
    }
?&gt;
Code checks if the "is_logged_in" data variable is set to true or false and displays the login box accordingly.

From what I can tell this works. Is there a better more efficient way to do this? and security wise should it be ok?

Thanks.


Messages In This Thread
Login Box and Logged In User data on each page. - by El Forum - 03-19-2011, 01:10 PM
Login Box and Logged In User data on each page. - by El Forum - 03-19-2011, 02:08 PM
Login Box and Logged In User data on each page. - by El Forum - 03-19-2011, 03:43 PM
Login Box and Logged In User data on each page. - by El Forum - 03-20-2011, 11:34 PM
Login Box and Logged In User data on each page. - by El Forum - 03-26-2011, 04:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB