0

I get the request headers from the browser like this.

<?php
$DataFromBrowser='';
$DataToBrowser='';

$headers =  getallheaders();
foreach($headers as $key=>$val){
  $DataFromBrowser = $DataFromBrowser .  $key . ': ' . $val . "\n";
}



//echo get_raw_http_request();
echo "Data from browser";
echo '<textarea rows="12" style="width:100%;">' .  $DataFromBrowser  . '</textarea>';

?>

And the output will look like this.

Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: foo=bar
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1

Now I want to do exactly the same but for the response headers from the server. I'm aware I can do the following but it will create another connection I need the response headers of the current served page.

<?php
$URL = 'https://www.google.com';

$headers = get_headers($URL);
foreach($headers as $value) {
    echo $value;
    echo "<br>";
}
?>

If I change the above to http://127.0.0.1 It get's it self in a deadlock and will loop until it bombs out.

The response headers should look like this.

HTTP/1.1 200 OK
Date: Wed, 08 Jun 2022 16:53:45 GMT
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.0.3
X-Powered-By: PHP/8.0.3
Content-Length: 589
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

5
  • What does "If I change the above to 127.0.0.1 It get's it self in a deadlock" mean? You have the power of "you control this code, on your own computer", so find out what it's actually doing: does it even load that script or does a completely different file kick in because you're already running a server on port 80? Does it make it past get_headers? Do you have warnings/errors turned on? etc. etc. Commented Jun 9, 2022 at 16:24
  • Do you mean headers_list()? Also check apache_response_headers() Commented Jun 9, 2022 at 16:26
  • 2
    You can not get the response headers for a request while inside that request on the server side. The web server that's wrapping the PHP call (and any intermediary caches/proxies) will add headers after PHP has completed its processing. Do you need all the headers? Or just those that PHP itself might be adding? Commented Jun 9, 2022 at 16:31
  • Please only use code snippets for html/css/javascript. Commented Jun 9, 2022 at 21:16
  • "If I change the above to http://127.0.0.1 It get's it self in a deadlock and will loop until it bombs out." - that would not perhaps be because the script code you have shown, is also triggered by requesting that very same URL ...? Commented Jun 10, 2022 at 7:56

1 Answer 1

1

Is it possible to get ALL response headers from from Apache using PHP?

No (if you are referring to the current request).


This was already answered in a comment above:

You can not get the response headers for a request while inside that request on the server side. The web server that's wrapping the PHP call (and any intermediary caches/proxies) will add headers after PHP has completed its processing.


See also the documentation for Apache on Headers:

This directive can replace, merge or remove HTTP response headers. The header is modified just after the content handler and output filters are run, allowing outgoing headers to be modified.

Except in early mode, the Header directives are processed just before the response is sent to the network.

https://httpd.apache.org/docs/trunk/mod/mod_headers.html#header


More specifically: You can't get the response headers which have been added by the Webserver (unless "early" is used)! (You might be able to get response headers which were added by PHP. You can get request headers which were added by the webserver.)

As for using "early": that is not really recommended (and if you use "early" for some headers in Apache you still won't necessarily get all headers in PHP.

As for sending another request via http://127.0.0.1 and getting the response headers for that - that does not make sense if you need the response headers for this request.

If you do not need the response headers in PHP, just look at them in the developer tools "Network" tab or use curl:

curl -IL https://example.org
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.