How can I access request headers that don't appear in $_SERVER?

后端 未结 3 1618
不知归路
不知归路 2020-12-28 19:09

I am attempting to create a REST API in PHP and I\'d like to implement an authentication scheme similar to Amazon\'s S3 approach. This involves setting a custom \'Authorizat

相关标签:
3条回答
  • 2020-12-28 19:27

    Try

    $_ENV['HTTP_AUTHORIZATION']
    

    When using CGI interface instead of Apache Module interface, HTTP headers should be available as environment variables.

    0 讨论(0)
  • 2020-12-28 19:34

    There is a fantastic PECL extension that allows for all sorts of HTTP related access. PECL_HTTP and more specifically http://php.net/http and http://php.net/manual/en/function.http-get-request-headers.php.

    0 讨论(0)
  • 2020-12-28 19:36

    You'll need to do some mod_rewrite wizardry to get your headers past the CGI barrier, like so:

    RewriteEngine on
    RewriteRule .? - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    <?php
        $auth = $_SERVER['HTTP_AUTHORIZATION'];
    ?>
    

    Note that if you're using mod_rewrite for other purposes, it could end up being $_SERVER['REDIRECT_HTTP_AUTHORIZATION'].

    0 讨论(0)
提交回复
热议问题