CodeIgniter Forums
New to CI, I need help with member area - sessions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: New to CI, I need help with member area - sessions (/showthread.php?tid=22373)



New to CI, I need help with member area - sessions - El Forum - 09-07-2009

[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


New to CI, I need help with member area - sessions - El Forum - 09-07-2009

[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.