Php doesn't get custom headers (Apache 2.4 + FPM / FastCGI php 7)

I tried to get all the headers using apache_request_headers (), $ _SERVER, $ _ENV and getallheaders ().

I know that Apache 2.4 resets unknown headers for security reasons and tries to get around it by adding:

SetEnvIfNoCase ^X (.*) HTTP_CUSTOM=$0 RequestHeader set HTTP_CUSTOM %{HTTP_CUSTOM}e env=HTTP_CUSTOM 

which successfully captures / renames known headers, but when trying to catch the X-Custom-Header it is always empty.

What could be the reason?

+5
source share
1 answer

So I'm not sure what you are trying to do.

If you are trying to add headers that begin with X from your request to your answer, I would use it in your htaccess file.

 Header echo ^X 

If you are trying to use the header values ​​in a PHP script, then they should be in your $_SERVER , but the names are normalized. for instance

 X-Custom-Header: blah X-Na-Bra: true 

can be accessed from

 <?php // note that headers are prefixed with "HTTP" and "-" and changed to "_" echo $_SERVER['HTTP_X_CUSTOM_HEADER']; echo $_SERVER['HTTP_X_NA_BRA']; // either way you should be able to find them with a print_r($_SERVER); // print_r(getallheaders()); should show the headers without normalized names $tempArray = getallheaders(); echo $tempArray['X-Custom-Header']; ?> 

It looks like you want to get values ​​from the dynamic number of headers that start with X. If so, then your code will not work anyway. Your code (if it works) will always contain the last value of the heading starting with X. So if you have more than 1 heading that starts with X, then you will be missing values. Using your code, you need to create a rule for each header that you want to pass to your PHP script, which sounds like a pain.

If I missed something, then comment below and I will update this answer.

0
source

Source: https://habr.com/ru/post/1260695/


All Articles