Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the http headers from current request in PHP

Is it possible to get the http headers of the current request with PHP? I am not using Apache as the web-server, but using nginx.

I tried using getallheaders() but I am getting Call to undefined function getallheaders().

like image 794
Justin Avatar asked Nov 05 '12 00:11

Justin


People also ask

How do I read any request header in PHP?

Receiving the request header, the web server will send an HTTP response header back to the client. Read any request header: It can be achieved by using getallheaders() function. Example 2: It can be achieved by using apache_request_headers() function.

How do I view headers in PHP?

The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request.

How do I get all headers?

you can use getallheaders() to get an array of all HTTP headers sent. Show activity on this post. Every HTTP request header field is in $_SERVER (except Cookie ) and the key begins with HTTP_ . If you're using Apache, you can also try apache_request_headers .


1 Answers

Taken from the documentation someone wrote a comment...

if (!function_exists('getallheaders'))  {      function getallheaders()      {         $headers = array ();         foreach ($_SERVER as $name => $value)         {             if (substr($name, 0, 5) == 'HTTP_')             {                 $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;             }         }         return $headers;      }  }  
like image 82
Layke Avatar answered Oct 03 '22 07:10

Layke