Bob II <he**@there.tv> wrote in
news:1108700375.9347916c4578683d24579eb003e8f3c7@t eranews:
Hey, I've never used PHP before. I was wondering if it is possible to
use PHP to generate web stats. My web host's stats aren't very good.
So I want to generate some custom stats for myself.
At a minimum, I want a log of the IP, referrer, environment variables,
time stamp, etc., and dump it into a comma delimited text file that I
can import into access or Excel. I don't need to generate gifs with
PHP. Just need the raw data.
Can PHP do this? My current understanding of PHP is that it is an
embedded language. I was hoping to execute a server-side script
without having any code embedded into the webpage. Maybe I could link
to a PHP file?
I'm a bit late to this thread, but I noticed talk had swayed to raw
access_logs. With PHP, you can get or reasonably estimate all of the info
you need to generate a simulated raw server log, which you could then
feed into any number of existing webstats apps, such as Webalizer. The
benefit here is that you'll get all the pretty charts and graphs and
hostname grouping and browser version grouping, etc. that a program like
this gives you.
Actually, this sounded like an interesting way to spend a few minutes, so
here you go, a raw log "faker":
<?php
#Open log file
if(!$fp = fopen('access.log', 'a')){
die('Unable to open access.log file for appending.');
}
#Build standard Apache-style log string
$username = (isset($_SERVER[PHP_AUTH_USER])) ? $_SERVER[PHP_AUTH_USER] :
'-';
$referer = (isset($_SERVER[HTTP_REFERER])) ? $_SERVER[HTTP_REFERER] :
'-';
$useragent = (isset($_SERVER[HTTP_USER_AGENT])) ? $_SERVER
[HTTP_USER_AGENT] : '-';
$entry = "$_SERVER[REMOTE_ADDR] - $username [" . date('d/M/Y:H:i:s O') .
'] "' . $_SERVER[REQUEST_METHOD] . ' ' . $_SERVER[REQUEST_URI] . ' ' .
$_SERVER[SERVER_PROTOCOL] . '" 200 ' . filesize(getcwd() . '/' . basename
($_SERVER[PHP_SELF])) . ' "' . $referer . '" "' . $useragent . "\"\n";
#Write log string to file
fwrite($fp, $entry);
?>
Put that code into a file named something like "logger.php" and create a
file named "access.log" in the same directory. Chmod the access.log file
to 777 (or "Owner RWX, Group RWX, World RWX") via your FTP app. Now, at
the top of your existing pages, insert this statement:
<?php include('logger.php'); ?>
If your files are currently .htm/.html, you may have to rename them to
..php for this to work, it depends on the server setup. Once everything is
set up properly, each visit to your pages will result in a new entry
being added to the "access.log" file. You can download this file
periodically and run it through something like Webalizer.
One caveat which was already pointed out is that this will only log hits
to your pages themselves, not hits to images, or any page that doesn't
use the include() statement. Another caveat is that the data size field
(i.e. the number of bytes that Apache sent to the remote user) is
estimated as the actual size of the file. AFAIK there's no way to pull
the real data, because while your script is running, Apache doesn't yet
know how big the output will be.
If you really want the CSV files, you can modify the code above so that
it prints out the right values, but with commas between them instead of
the Apache log formatting.
hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources