: Capturing client request headers (complete) along with IP address information? I want to write client request to a file with two things/information headers ip address The code I'm testing on
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'])) {
$ip = $_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.
Thanks.
More posts by @Gretchen104
2 Comments
Sorted by latest first Latest Oldest Best
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.
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.
www.openwebanalytics.com/ http://piwik.org/
You can find some other options here.
www.sitepoint.com/10-web-analytics-packages-for-tracking-your-visitors/ http://www.makeuseof.com/tag/8-free-tools-live-website-visitor-tracking/
I highly recommend looking into www.openwebanalytics.com/ and 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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.