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

[eluser]thewindsurfer[/eluser]
Hello,

I'm just starting out with codeigniter and I was wondering what the proper way to put a login box on each page would be.

I have already created a regular login page that redirects to a members area. So I understand how that part works.

Now instead of a login page i want a small login box on each of my pages. And when the user logs in I want that box to show there username and have a logout button.

What is the proper way to implement this. I know I can do it in the view and just check if the login session is set. Then either display the login box or the user info box. But I feel like that wouldn't good practice.
#2

[eluser]CroNiX[/eluser]
There are many ways, but the easiest probably is a base controller or a library that gets autoloaded which would check the login state on each pageload automatically. I generally have what I call mini views, of which one would be a login form, which is part of my template and gets autogenerated on every page page reflecting the current login status/options.
#3

[eluser]thewindsurfer[/eluser]
Thanks for the quick reply.

I actually already did a controller part where I have a function call in the construct that checks if the proper session is set.

I am curious about this miniview that you spoke of. Is that basically just an include on the theme (like header)?. And what code does that have?

Would I make my controller function return TRUE or FALSE if a user is logged in? And then call that function from my "miniview" and create a the proper form accordingly with an if else?
#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.
#5

[eluser]Varadha[/eluser]
Hi,

I am a beginner in code igniter. I have my controllers basic setup as follows:

application/core/MY_Controller.php
-----------------------------------
Code:
class MY_Controller extends CI_Controller {
    
    function __construct(){
        parent::__construct();                
    }
    
}

application/controllers/Admin_Controller.php
---------------------------------------------
Code:
class Admin_Controller extends MY_Controller {
    
    function __construct(){
        parent::__construct();
        $this->is_logged_in();
    }
    
    function is_logged_in()
    {        
        $admin_logged_in = $this->session->userdata('admin_logged_in');
        if(!isset($admin_logged_in) || $admin_logged_in != true)
        {
            $data['main_content'] = 'admin_login_view';
            $this->load->view('includes/admin_template', $data);
        }        
    }    
}

application/controllers/admin.php
---------------------------------
Code:
class Admin extends Admin_Controller {
    
    function __construct()
    {
        parent::__construct();        
    }    
    
    function login()
    {
        $data['main_content'] = 'admin_login_view';
        $this->load->view('includes/admin_template', $data);
    }
    
    function members_area()
    {
        $data['main_content'] = 'admin_welcome_view';
        $this->load->view('includes/admin_template', $data);
    }
}

It works fine with login credentials. If i access the members_area() without login, it just loads the welcome view. I have to include many number of functions after login. Should i verify the login session for each and every function? Is there any other way to solve this? I need help on this. Please guide me :question: .

Regards
Varadha




Theme © iAndrew 2016 - Forum software by © MyBB