Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching custom Authorization header from incoming PHP request

So I'm trying to parse an incoming request in PHP which has the following header set:

Authorization: Custom Username 

Simple question: how on earth do I get my hands on it? If it was Authorization: Basic, I could get the username from $_SERVER["PHP_AUTH_USER"]. If it was X-Custom-Authorization: Username, I could get the username from $_SERVER["HTTP_X_CUSTOM_AUTHORIZATION"]. But neither of these are set by a custom Authorization, var_dump($_SERVER) reveals no mention of the header (in particular, AUTH_TYPE is missing), and PHP5 functions like get_headers() only work on responses to outgoing requests. I'm running PHP 5 on Apache with an out-of-the box Ubuntu install.

like image 377
lambshaanxy Avatar asked May 25 '10 07:05

lambshaanxy


1 Answers

For token based auth:

  $token = null;   $headers = apache_request_headers();   if(isset($headers['Authorization'])){     $matches = array();     preg_match('/Token token="(.*)"/', $headers['Authorization'], $matches);     if(isset($matches[1])){       $token = $matches[1];     }   }  
like image 122
deepwinter Avatar answered Oct 02 '22 11:10

deepwinter