views:

6329

answers:

6

I am writing a web-app PHP. I want to use MVC pattern for this, and decided to go with CodeIgniter.

My application will have some pages which will require authentication, some pages wont.

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?

A: 

Why don't you use sessions?

If you are on a page that require authentication check on the session if your already logged on.

I know that some people prefer using plugins but in this case the use of sessions could be the simplest and fastest way.

nicruo
+2  A: 

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

rick
+4  A: 

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/

sanchothefat
i've always put the log in / log out functions (as well as a few others) into an "auth" controller. In any individual controller i can check if the user's logged in by seeing if $this->session->userdata('uid') is set. Is that bad practice?
Mala
A: 

I was looking into the same thing recently, and I found a CodeIgniter fork called Kohana that includes a nice authentication module. If you are set on CI, maybe adapting Kohana's auth module backwards to CI would save you some time? If you have just started out on your project and PHP5 is OK to use, consider switching over; they are very similar frameworks.

Barnabas Kendall
A: 

May be you can use CL_AUTH library for CI. I've used it and it works good. You can find it here http://www.jasonashdown.co.uk/cl_auth_doc/

A: 

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

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

pbreitenbach