[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!";
}
}
?>
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.