?
Solved

Alternative to apache_request_headers

Posted on 2010-11-30
13
Medium Priority
?
1,587 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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 2000 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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