Jeroen Moors

blog

Simple CodeIgniter module for user session management
Written on 14 October 2013

Recent I got a question how to connect my oAuth module to Tank Auth for user session management. While that might be possible, I think that would be overkill.

When I wrote the oAuth module, I also wrote a very simple module to handle the user session management. The module has only 3 methods:

Set the current user id

After your authentication process, being it oAuth or any thing else, use $this->current_user->setId(10, true); to set the current logged in user id to 10. The $rememberMe parameter defines if the user has to login or not the next time he or she visits the site.

For this functionality a cookie use used on the client side and a table named user_autologin on the server.

Get the current user id

Calling $userId = $this->current_user->getId(); will store the current logged in user in $userId or false if there is currently no active user session.

Log the current user off

To end a session use $this->current_user->logoff();.

For the Remember Me functionality to work you will need to create a table called user_autologin:

The code can be downloaded here. As always all feedback is welcome!