标头Rest Api中的身份验证

时间:2018-12-14 08:42:49

标签: php rest codeigniter authentication

我正在使用 codeigniter 中的rest api,我想添加api-authentication,为此,我要发送 access_token位于邮递员的标头部分,但我想知道如何在.php文件中获取它。 我只希望尝试在没有access_token或错误的access_token的情况下访问api的人不应获得api结果。 在我的controller中,输入以下代码。如何在rest api上添加身份验证?

if ($access_token!= $_POST['access_token']) {
   echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";

} else {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'any text';
}

1 个答案:

答案 0 :(得分:-1)

如果要在codeigniter中创建RESTful服务器,请考虑使用https://github.com/chriskacerguis/codeigniter-restserver库。

要在codeigniter中实现API身份验证,请考虑使用https://github.com/ParitoshVaidya/CodeIgniter-JWT-Sample