Solved

Alternative to apache_request_headers

Posted on 2010-11-30
13
1,573 Views
Last Modified: 2012-05-10
I need to capture the entire browser header and would normally have used apache_request_headers.  However the server where I need to do this is using suPHP and apache_request_headers is not available.  Are there alternatives that will capture the entire header that I could use?
0
Comment
Question by:duz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 4

Expert Comment

by:max-hb
ID: 34240130
You can use header_sent() to verify if your script has already sent headers. To find out which headers were sent (or prepared for sending) use headers_list().
0
 
LVL 4

Expert Comment

by:max-hb
ID: 34240139
Typo: It should be headers_sent() NOT header_sent() ;-)
0
 
LVL 24

Author Comment

by:duz
ID: 34240308
I can't use headers_sent() because I need the browser header.  
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 4

Expert Comment

by:max-hb
ID: 34241006
O.K., totally misunderstood your question ;-)
To read header info coming from the browser you should have a look at the global var $_SERVER which contains some information about the client side:
<?php
echo "<pre>";
var_dump($_SERVER);
echo "</pre>";
?>

This will not allow to fetch all headers but maybe its enough for your purpose.
0
 
LVL 24

Author Comment

by:duz
ID: 34242283
Thanks but as I said originally "I need to capture the entire browser header...".
0
 
LVL 9

Expert Comment

by:absx
ID: 34246600
Hi,

Here's a slightly modified example from php.net. It fetches only the HTTP header values from the $_SERVER array and tries to format the key names to match apache_request_handlers() format.


if( !function_exists('apache_request_headers') ) {
function apache_request_headers() {
  $arh = array();
  $rx_http = '/\AHTTP_/';

  foreach($_SERVER as $key => $val) {
    if( preg_match($rx_http, $key) ) {
      $arh_key = preg_replace($rx_http, '', $key);
      $rx_matches = array();

      // do some nasty string manipulations to restore the original letter case
      // this should work in most cases
      $rx_matches = explode('_', $arh_key);
      if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
        foreach($rx_matches as $ak_key => $ak_val) 
          $rx_matches[$ak_key] = ucfirst(strtolower($ak_val));
        $arh_key = implode('-', $rx_matches);
      }
      $arh[$arh_key] = $val;
    }
  }
  return( $arh );
}
}

Open in new window

0
 
LVL 9

Expert Comment

by:absx
ID: 34246604
Typofix: meant to say apache_request_headers(), not apache_request_handlers().
0
 
LVL 24

Author Comment

by:duz
ID: 34246779
Thank you absx.

I believe that $_SERVER is not as reliable or consistent across servers as apache_request_headers and will often omit data?

Is this the best that can be done, should I look outside PHP for a solution?
0
 
LVL 9

Expert Comment

by:absx
ID: 34246911
Hi,

I guess there must be differences in, say, X-FORWARDED-FOR headers and such. It'd probably be best to look up the differences per server, though. There's not too many ways of serving PHP pages, since they only provide modules for IIS and Apache and the rest are served by CGI implementations.

Which parts of the information do you really really need? If it's just user agent, I'd trust the $_SERVER data (and probably do further tests in JS, since it can be easily faked.

There's also rumors of a $GLOBALS["HTTP_RAW_POST_DATA"] being available for HTTP POST requests (see http://us.php.net/manual/en/reserved.variables.httprawpostdata.php), but at least on my Apache/FCGI configuration I couldn't get this populated.

0
 
LVL 9

Expert Comment

by:absx
ID: 34246930
Also, you could use Fiddler (http://www.fiddler2.com/fiddler2/version.asp) to test whether all the headers are captured by $_SERVER from different browsers. In my quick test, all the HTTP headers sent by Firefox were indeed displayed by $_SERVER.
0
 
LVL 24

Author Comment

by:duz
ID: 34247064
Thx absx for your helpful comments.

I am not at liberty to say exactly what information I need from the header but I do need all of it exactly as it was sent. I can say that the data will undergo forensic examination, which is why I need the complete header.
0
 
LVL 9

Accepted Solution

by:
absx earned 500 total points
ID: 34247211
Well, that does sound like it'd be best to look at the logging capacities of the HTTP server in question. The server software would be the only thing that knows for sure, unless some transparent proxy/firewall is sat in front of it.
0
 
LVL 24

Author Comment

by:duz
ID: 34275202
I think you are right absx

Thank you for your help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question