I want to write client request to a file with two things/information

  • headers
  • ip address

The code I'm testing on free hosting service.Code is

$headers= apache_request_headers();
foreach ($headers as $header => value)
echo "4header: $ value </br >\n";

Above, work for headers; but I'm confused what does apache_request_headers() means its given as example in offical php website. Does it work for apache only?

If, the request header doesn't contain IP address I want to capture it as under

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
} else {
    $ip = $_SERVER['REMOTE_ADDR'];

Merging the two, I want to write in single file and append it with time information for every request?. If time info is included as part of request...then its not required for new code.

Also, please suggest what the best way to capture such request, be it in separate php which is reading all the request to particular page or directory ; or put it inside the i.e index.html page. Thanks.


  • Isn't all this in the log file? – closetnoc Apr 18 '14 at 4:15
  • log file but I'm using free hosting service? it isn't my own machine – asadz Apr 18 '14 at 4:21

I am not answering your code question directly based upon your comments. I think I understand what you want. I feel that you can be better served if I go in another direction. I hope you don't mind.

Why recreate the wheel?

Most people use Google Analytics though there is some confusion over the data from time to time. It is free.

Two excellent options are also free.

You can find some other options here.

I highly recommend looking into http://www.openwebanalytics.com/ and http://piwik.org/. These look excellent and it seems like the interface and data is pretty straight forward.

Otherwise, you will be coding and not really happy with the results.


As closetnoc says, you don't necessarily need to reinvent the wheel. However, there can be some benefits to your own logging if that is the route you want to take (and you have the time). Anyway, to answer some of your queries...

Does it [apache_request_headers] work for apache only?

Primarily yes. It isn't available on IIS for instance. However, as stated in the docs (which you reference), it is available on some other systems, and not necessarily just under an Apache Module (in PHP 5.4+).

But even if this function is not available, this information is easily extracted from the $_SERVER superglobal. All elements starting HTTP_ have been extracted from the HTTP request headers.

If, the request header doesn't contain IP address I want to capture it as under

The request headers do not contain the IP address. In fact you should not trust it if it is, as it is easily faked. The only IP address you can trust is $_SERVER['REMOTE_ADDR'] - and this is the value you should be using "in the wild". The other IP addresses that might appear as part of the request headers can only be used in specific circumstances on known systems.

If time info is included as part of request

The time is not part of the request headers. The "time" is the time you are processing the request / running your script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.