• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to CI, I need help with member area - sessions

#1
[eluser]rkitkonsult[/eluser]
Hi,
I’m all new to CI, what I’ve seen so far is really nice!

I have started to try things out. The first problem I have is about sessions. I want a member to be able to login (then starting a session), and for each member page I have to check the session to see that its a loged in user, otherwise redirect. How do I do this in a proper way? Is there anyone having a sample application that handles this?

/Robert

#2
[eluser]kurucu[/eluser]
If you autoload the session library, a session is actually started for every unique visitor.

Then, in your code, you can mark a unique user as logged in once they have succesfully completed and submitted your login form, with
Code:
$this->session->set_userdata('userid', $user_id_from_authentication);

Somewhere in your members area controller, you can:
Code:
if( $this->session->userdata('userid') === FALSE )
{
    redirect('/not/allowed');
}

The session library (or is it a helper?) returns false if a userdata variable has not been set, hence the code above. When a user logs out be sure to unset 'userid' and/or any other variables you set for the user session.

What I would suggest is that you at least turn on cookie encryption, and preferably database variable storage. The least information sent about authentication over the network, the better.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.