[PHP] header_check.php

Will

Senior Administrator
Staff member
Joined
Mar 4, 2012
Posts
8,197
Location
%tmp%
I did not write this script, it's from a book I'm reading. It's meant to display what details are visible to a website from your browser. The code and upload location is below. However, I'm getting an error message when I access the location.

Code:
Fatal error: Call to undefined function apache_request_headers() in /var/sites/w/willwatts.co.uk/public_html/header_check.php on line 3

I don't know PHP, although I can understand what's going on in the script. Is this something to do with PHP versions, or am I missing a file somewhere else on my site that is needed?

[NO-PARSE]http://willwatts.co.uk/header_check.php[/NO-PARSE]

Code:
<?php

$get_headers = apache_request_headers();

echo $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI'] . " " . $_SERVER['SERVER_PROTOCOL'] . "<br/>";

foreach ($get_headers as $header => $value) {
    echo "$header: $value <br/>\n";
}

echo "<br/><br/>Your IP address is: " . $_SERVER['REMOTE_ADDR'];

?>
 
Not sure why the error is there, I have just uploaded the sample code to my webspace and it appears to work as it should. Not sure why it appears with an error as it does on your domain, as you say, poss phph versions - which vers are you running, I have v5.4 running?
 
Ah, I see. PHP version is 5.3, not sure about any other info.

How can I fix it? Probably my lack of sleep, but I can't see what I'm meant to add/change to make it work.
 
Apache is installed. I've replaced this line: $get_headers = apache_request_headers();

with:

$get_headers = ($_SERVER['HTTP_HOST']);

The full code is now:

Code:
<?php

$get_headers = ($_SERVER['HTTP_HOST']);

echo $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI'] . " " . $_SERVER['SERVER_PROTOCOL'] . "<br/>";

foreach ($get_headers as $header => $value) {
    echo "$header: $value <br/>\n";
}

echo "<br/><br/>Your IP address is: " . $_SERVER['REMOTE_ADDR'];

?>

This seems to get me a little further.
Warning: Invalid argument supplied for foreach() in /var/sites/w/willwatts.co.uk/public_html/header_check.php on line 7
 
Apache is installed. I've replaced this line: $get_headers = apache_request_headers();

with:

$get_headers = ($_SERVER['HTTP_HOST']);

The full code is now:

Code:
<?php

$get_headers = ($_SERVER['HTTP_HOST']);

echo $_SERVER['REQUEST_METHOD'] . " " . $_SERVER['REQUEST_URI'] . " " . $_SERVER['SERVER_PROTOCOL'] . "<br/>";

foreach ($get_headers as $header => $value) {
    echo "$header: $value <br/>\n";
}

echo "<br/><br/>Your IP address is: " . $_SERVER['REMOTE_ADDR'];

?>

This seems to get me a little further.
Warning: Invalid argument supplied for foreach() in /var/sites/w/willwatts.co.uk/public_html/header_check.php on line 7

Sorry I am out of town so cannot be too helpful...

I am not sure if there is a $_SERVER array that is always created with PHP...

The problem with your foreach loop is that it's trying to treat get_headers as a multi dimensional array which I do not believe it is currently...

If you want you can do some manually scripting just looking up all the $_server constants and spitting them out...

It's kind of cool to see the information tracked by the servers...

I will be back in town tomorrow but I need to catch up on schooling from being gone so I can help after friday if you would like :grin1:
 
Did some quick research and found some code which will fix your issue in the first post. Paste it in the HTML before your first bit of PHP.

It simply checks if that function isnt already on the server and creates it if not.

Code:
<?php
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($ak_val);
        $arh_key = implode('-', $rx_matches);
      }
      $arh[$arh_key] = $val;
    }
  }
  return( $arh );
}
///
}
///
?>

Source: http://stackoverflow.com/questions/2916232/call-to-undefined-function-apache-request-headers
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top