vote up 2 vote down star
4

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?

flag

68% accept rate

5 Answers

vote up 3 vote down check

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/

link|flag
vote up 2 vote down

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

link|flag
vote up 0 vote down

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.

link|flag
vote up 0 vote down

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.

link|flag
vote up 0 vote down

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/

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.