PHP 中的 REST 身份验证 (CodeIgniter)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2796950/
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
REST Authentication in PHP (CodeIgniter)
提问by zidane
I writing REST API form my web application. Application is written using CodeIgniter framework. Application itself is working fine, but I'm stuck on making REST Authentication. I think that basic Http Authentication will be good enough for some time. Public API is not yet planned.
我从我的 Web 应用程序中编写 REST API。应用程序是使用 CodeIgniter 框架编写的。应用程序本身运行良好,但我坚持进行 REST 身份验证。我认为基本的 Http 身份验证在一段时间内就足够了。公共 API 尚未计划。
Is there any code example how to achieve REST Authentication so after user is authenticated he can freely call all protected methods.
是否有任何代码示例如何实现 REST 身份验证,以便在用户通过身份验证后,他可以自由调用所有受保护的方法。
回答by Phil Sturgeon
I have written up a REST Controller to make your REST applications easier to build. You can read all about it on NetTuts: Working with RESTful services in CodeIgniter.
我编写了一个 REST 控制器来让你的 REST 应用程序更容易构建。您可以在NetTuts上阅读所有相关内容:在 CodeIgniter 中使用 RESTful 服务。
回答by ZZ Coder
If you use HTTPS, you can use Basic authentication and it's very easy to do. Just add following code to your controller,
如果您使用 HTTPS,您可以使用基本身份验证,这很容易做到。只需将以下代码添加到您的控制器,
   if (empty($this->input->server('PHP_AUTH_USER')))
   {
       header('HTTP/1.0 401 Unauthorized');
       header('HTTP/1.1 401 Unauthorized');
       header('WWW-Authenticate: Basic realm="My Realm"');
       echo 'You must login to use this service'; // User sees this if hit cancel
       die();
    }
    $username = $this->input->server('PHP_AUTH_USER');
    $password = $this->input->server('PHP_AUTH_PW');
    // Check username and password
I use mod_php, your auth variable names maybe different if using other SAPI modules.
我使用 mod_php,如果使用其他 SAPI 模块,您的身份验证变量名称可能会有所不同。

