解决:PHP获取Authorazition的值

430 阅读1分钟

最近做个项目要获取app端传过来的token,通过请求头中的authorization传输,发现通过$_SERVER中接收不到(apahce),下面是解决方案:先确定Apache服务器开启rewrite_module模块,.htaccess文件中添加如下:

Options +FollowSymlinks -Multiviews
RewriteEngine On
#Authorization Headers
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

如已开启rewrite_module模块,忽略上面代码前两行。

然后就可以用通过 $_SERVER['HTTP_AUTHORIZATION'] 接收值了。

以上方法我在本地环境没问题(windows),但是到了我们测试环境(Linux),上面的配置不起作用,卡了我两天,后找到解决方法就是用下面的函数

apache_request_headers()

代码可参考下面:

if(function_exists('apache_request_headers')){
    $requestToken = apache_request_headers()['Authorization'];
}

问题解决!