php 使用 CodeIgniter 进行用户身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/457497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
User authentication with CodeIgniter
提问by Sabya
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?
我正在使用 PHP 编写 Web 应用程序。我想为此使用 MVC 模式,并决定使用CodeIgniter。我的应用程序将有一些页面需要身份验证,而有些页面则不需要。我想以一种非常通用的方式设计它,这样就不应该有代码重复。任何人都可以为此指出一些好的“设计/类结构”吗?
采纳答案by roborourke
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/
在代码点火器 wiki 上搜索“身份验证”,有许多结果可能会有所帮助:http: //codeigniter.com/wiki/
回答by pbreitenbach
"Ion Auth" is lean, well-programmed, somewhat widely used and is actively maintained.
“Ion Auth”精简、编程良好、使用广泛并积极维护。
回答by rick
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.
如果“某些页面”是指某些控制器(视图的网关),那么您可能需要研究控制器继承。使用您自己的控制器扩展默认的 CodeIgniter 控制器,并在构造函数中进行身份验证检查(检查会话是否有登录标志或其他内容,如果未登录,则重定向到登录页面)。然后,所有需要身份验证的控制器都需要扩展新的父控制器。就是这样。
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
前往 CodeIgniter 论坛并搜索一些不同的方法来扩展控制器。这是一个http://codeigniter.com/forums/viewthread/89768/#452890
回答by Barnabas Kendall
I was looking into the same thing recently, and I found a CodeIgniter fork called Kohanathat 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.
我最近也在研究同样的事情,我发现了一个名为Kohana的 CodeIgniter fork ,它包含一个很好的身份验证模块。如果您设置为 CI,也许将 Kohana 的 auth 模块向后适应 CI 会为您节省一些时间?如果你刚刚开始你的项目并且PHP5可以使用,可以考虑切换;它们是非常相似的框架。
回答by Barnabas Kendall
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/
也许您可以将 CL_AUTH 库用于 CI。我用过它,效果很好。你可以在这里找到它http://www.jasonashdown.co.uk/cl_auth_doc/
回答by Luca Mori Polpettini
Visit GitHuband search for Codeigniter Auth or Authentication, or check the CodeIgniter Wiki, you'll find many libraries with different features.. explore them and choose the one you need! But be careful, many are for CI 2, and you have to ucfirst the classesto use with CI 3, otherwise they don't work at all.
访问GitHub并搜索 Codeigniter Auth 或 Authentication,或查看CodeIgniter Wiki,您会发现许多具有不同功能的库......探索它们并选择您需要的!但是要小心,许多是针对 CI 2 的,并且您必须首先将这些类与 CI 3 一起使用,否则它们根本不起作用。
回答by Arjun Kariyadan
Use flexi autha modified version of the popular Ion Authlibrary. It's more advanced and do all the job out-of-the-box.
使用flexi auth是流行的Ion Auth库的修改版本。它更先进,可以开箱即用地完成所有工作。
flexi auth is a free open source user authentication/login library for use with the CodeIgniter 2.0+ framework.
flexi auth 是一个免费的开源用户身份验证/登录库,用于 CodeIgniter 2.0+ 框架。
I know it's too late but I hope someone else will find it helpful. Cheers!
我知道为时已晚,但我希望其他人会发现它有帮助。干杯!

