Welcome Guest, Not a member yet? Register   Sign In
Login system - handling same view that is different for user and guest


So straight to the point, i'll be using example:
Let's say i want to build an e-book app, that on main page (when user is not logged in)shows list of popular e-books, and some other default information. When user logs in, some of the content change, but some remains the same (for example user mini panel shows up, list result are based on user preferences etc.).

How should i handle a login system like this?

I know that best practice is to extend my CI_Controller to check for login status, but that's the part i understand and agree with. But later on, should my every controller (for examples purpose let's assume that every view has it's logged_in/logged_out state) have if statement, that if TRUE it calls view sending some user parameters, but if not, it calls same view without those paremeters. Is that the right approach? Is there away to avoid that repeating same if login statement in every controller to determine how the view should be called?

Thanks in advance,
Best regards

Yes, check for the logged in/out status MY_Controller, and have all other controllers extend it. You then have a choice of two options:

1. Loading a logged in or out view direct from your controllers depending on the logged in/out status.
2. Load the same view from your controller, but inside that view control what is being displayed to logged in/out users (this is my preference as option one creates 2 views for every page, updating something shared means you have to update it in two places, not very DRY).

To achieve the second option, in MY_Controller you can load the logged in/out status into a variable and load it directly into all views using this line of code


Then if your views it's a simple matter of displaying / not displaying content to users depending on their status. In MY_Controller, lets say if logged in $logged_in = true; if not logged in $logged_in = false; and that you've loaded it by using the above code snippet. In your view it's now a simple matter of
<?php if($logged_in): ?>
   // display something for the logged in user
<?php endif; ?>

<?php if(!$logged_in): ?>
   //display something for not logged in user
<?php endif; ?>

<?php if($logged_in): ?>
   //display something for logged in user
<?php else: ?>
   //else display something else
<?php endif; ?>

Thanks a lot. I was suspecting that we're kinda limitted to if else statement to determine output content.

You're not "limited", it's just how coding works (well, until quantum computing comes along).
At some point, somewhere, you have to make a "logged in/out" check and act upon the result of that check AKA if/else

I suppose what you could do is set a variable called "$logged, and when logged in it = "in", when logged out it = "out"

Then in your views you could put in all your "shared" content, and where you want to include something you could use


And organise your view folders like:


And then when you want some logged in stuff, in "views/in/some_sub_view1" you add it and in "views/out/some_sub_view1" you just keep the file empty.
And then when you have content for just logged out users you use the same process in reverse, make a new view, leaving the logged in file empty.
But... then you have to make a bunch of empty views, could be more confusing than it's worth. Depending on your situation.

Anyway, good luck with it!

Theme © iAndrew 2016 - Forum software by © MyBB