Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 689
Sabya Avatar asked Jan 19 '09 12:01

Sabya


3 Answers

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: http://codeigniter.com/wiki/

like image 183
roborourke Avatar answered Nov 14 '22 17:11

roborourke


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

http://github.com/benedmunds/CodeIgniter-Ion-Auth

like image 42
pbreitenbach Avatar answered Nov 14 '22 18:11

pbreitenbach


If by "some pages" you mean some controllers (the gateway to your views), then you may want to investigate controller inheritance. Extend the default CodeIgniter controller with your own and put an authentication check in the constructor (check the session for a logged in flag or something and if not logged in then redirect to login page). Then, all controllers that require authentication will need to extend your new parent controller. That's it.

Head on over to the CodeIgniter forums and search for some different ways to extend the controller. Here is one http://codeigniter.com/forums/viewthread/89768/#452890

like image 3
rick Avatar answered Nov 14 '22 18:11

rick