Welcome Guest, Not a member yet? Register   Sign In
Requesting user login information from my views
#1

[eluser]JohnDoerther[/eluser]
Hi guys,

I am currently implementing user profiles on a website, and the login system works great using Erkana.

Now, I use the constructor to check in every controller if the user is logged in or not, and depending on that, I redirect to a login page (depends on the page - some pages are protected, others are not...)

Now, in a header file that is included in every page (secured and not secured) I am looking for an easy way to check if a user is either loged in, or not.

This is what I have so far:

For the constructors with secured pages:

Code:
function User(){
        parent::Controller();
        $this->load->library('Erkanaauth');
        $this->load->helper('url');

        if (!$this->erkanaauth->try_session_login()) {
            redirect('front/account/login');
         }
    }

The non-secured pages do not have this code in the constructor.
Now, what I want to do, is know from my views if a user is logged in or not. I know I can do this by using

Code:
getField('email');

But that only seems to work for views called by protected controllers, so only pages where the user is actually logged in.

So in other words, I want an easy way to include user state in my views, both for secured pages and for unsecured pages.
I know I could do something like the following for every single function:

Code:
if (!$this->erkanaauth->try_session_login()) {
            redirect('front/account/login');
                    $data['loggedIn'] = true;
         }

and use variable
Code:
loggedIn
in my views, but there has to be an easier way that applies to the complete class instead of to each independent function.

Thanks in advance, I am really stuck on this...
#2

[eluser]Twisted1919[/eluser]
1.Extend the controller class with MY_Controller .
2.Define a public var in MY_Controller ($data) ;
3.Every controller will extend MY_Controller.
4.Set something like $this->data['logged_in'] = $this->session->userdata('logged_in); in MY_Controller constructor which by default is false .
Then after you execute the login, do a call like , $this->session->set_userdata('logged_in',TRUE); so that when the next check of $this->data['logged_in'] is made, it'll return true if the user is logged in .
Then, use $logged_in in your views .

L.E : Is this auth library maintained anymore ?
Even if it is, i suggest building one yourself .
#3

[eluser]JohnDoerther[/eluser]
Hi ,

Thanks for the reply - I created a controller MY_Controller, and my controllers now extend this - this seems to work.

What still does not work though is accessing a global variable in my views. I have this defined in MY_Controller:

Code:
<?php
    class MY_Controller extends Controller{
        
        function MY_Controller(){
            parent::Controller();
            global $loggedin;
            $this->load->library('Erkanaauth');
            $loggedin =  $this->erkanaauth->try_session_login();
        }
        
    }
?>

In my view, I use
Code:
if ($loggedin)

But I guess this is trying to get $data['loggedin'] which ofcourse is not defined in the MY_Controller.
So how can I easily access the global variable I declare in the MY_Controller from within my views?

Thanks a bunch!
#4

[eluser]pickupman[/eluser]
You can use in your constructor
Code:
$data['logged_in'] = $this->erkanaauth->try_session_login();
$this->load->vars($data); // make available in all views

In the context of OOP programing the use of global is not really needed. You can also create a variable/property for class, and it is available for the entire class/controller
Code:
class User extends Controller{

  var $some_var; //create a property for controller

  function User(){
    parent::Controller();
    $this->some_var = 'something always available';
  }

   function index()
     echo $this->some_var;
   }
}
#5

[eluser]JohnDoerther[/eluser]
Works like a charm thanks a bunch!




Theme © iAndrew 2016 - Forum software by © MyBB