Welcome Guest, Not a member yet? Register   Sign In
how to structure logindata in a header
#1

[eluser]Sweden[/eluser]
Hello!
I’m not sure how to structure this code...

I have a header which I put some details in if you are logged in, for example “Welcome, Sweden”, and a login box if you’re not logged in.

This is obviously printed in the view, and on every page, but how should I check if a user is logged in?

Some things I thought of is either:
-On every page, send a “loggedIn” bool to the header (along with the details such as userid,username) (doesn’t seem that good since it needs on every page)
-Call the user_model from the view (which I’ve read is not a good way to structure things)

The user_model is auto loaded by CI’s config.

Whats your toughts about this?
#2

[eluser]Govinda[/eluser]
Hi Sweden

you just use sessions.. and CI had a nice class for that. Please read about sessions and use them.

You build an authentication system where the user logs in.. and if their input matches a valid user record then you update that user's session with various data, like: their name ("Sweden"), and the fact that they are logged in (TRUE), and the access level they have (e.g. "manager", or "admin"... depending on the levels of access you app needs). You do all your business logic in the controller. In the controller is where you check if they are logged in ((you check by looking what is in the session), and what view file (or includes, or parts of views, view includes, etc) you load for that user/page load (depending on what is in the session, e.g. their access level).

Like in one of my apps, I have this code fire after the user submits an appropriate username/password:

Code:
$alterSessdata = array(
   'userID'   => $userData->userID,
   'UserAccess'  => $userData->access,
   'userAccess_KEY' => $userSession_access_KEY,
   'isloggedIn'    => TRUE
   );
$this->session->set_userdata($alterSessdata);

HTH
-Govinda




Theme © iAndrew 2016 - Forum software by © MyBB