User authentication with CodeIgniter


I am writing a web application using PHP. I want to use the MVC pattern for this, and decided to go with CodeIgniter. My application will have some pages which will require authentication, and some pages won't. I want to design this in a very generic way, so that there should be no code duplication. Can any one point to some good "design/class structure" for this?

2/10/2013 11:30:17 AM

Accepted Answer

Write a custom library that you can autoload in your code igniter app on every page view. It should have functions that:

  • Authenticate the user ie. check if a user is logged in or not
  • Log you in ie. set a session variable or something
  • Log you out

Then in your controller classes you can do a call to the authentication function in the constructor then depending on the outcome continue as normal or redirect them to a login screen with an access denied message.

Do a search on the code igniter wiki for 'authentication' and there are a number of results that may help:

1/19/2009 6:12:14 PM

"Ion Auth" is lean, well-programmed, somewhat widely used and is actively maintained.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow