Welcome to WebmasterWorld Guest from 54.158.248.112

Forum Moderators: coopster & jatar k

Possible PHP issue with reading Apache Request Header Info

     
1:38 am on Jul 6, 2018 (gmt 0)

Preferred Member from CA 

Top Contributors Of The Month

joined:Feb 7, 2017
posts: 406
votes: 33


This piece of code came from the Apache sub-forum in this thread: [webmasterworld.com...] It is included into each web page so that the requestor's header info is logged to an external file, where we can use it to determine if the requester is a bot or human.

The issue is that the code seems to truncate the input once it hits the first blank space or " ". For example, a piece of information called a User Agent is available from the raw access log, another Apache log file. This information should be identical in the external file written by this script, but the php code seems to cut off the User Agent after the first " ".

Raw access log User Agent:
Mozilla/5.0 (Linux; Android 7.0; SM-G390W Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36

Header Log User Agent:
Mozilla/5.0 


Can someone look at this code and tell me if there is anything in there that would truncate the input string after the first blank space " "?

Here is an example of a snippet of the output file:
2018-07-04:01:27:03
URL:/example/tag/gasoline/
IP:180.76.15.138
Accept:*/*
Accept-Encoding:gzip
Accept-Language:en-US
Connection:close
Host:example.com
User-Agent:Mozilla/5.0


I hope I am explaining myself sufficiently. Please ask for any clarification. Thanks in advance.

<?php
function get_server($var)
{
return isset($_SERVER[$var]) ? $_SERVER[$var] : false;
}

if (!function_exists('getallheaders'))
{
function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{ $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; }
}
return $headers;
}
}

$ip = get_server('REMOTE_ADDR');
$fh = fopen($_SERVER['DOCUMENT_ROOT'] . "/boilerplate/headers-". date('Ymd') . ".log","a");
fwrite($fh, date('Y-m-d:') . date("H:i:s\n"));
$thispage = $_SERVER['REQUEST_URI'];
fwrite($fh, "URL: $thispage\n");
fwrite($fh, "IP: $ip\n");

foreach (getallheaders() as $name => $value)
{
fwrite($fh, "$name: $value\n");
}

fwrite($fh, "----\n\n");
fclose($fh);
?>