Web Stats With PHP?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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 am currently using Go Daddy hosting--the $3.95/month package.  It's nice
and cheap, but the stats aren't very good.  I just basically want some raw
info that I can import and analyze as I need.  I don't get raw data.  Just

With my package, Perl, CGI, and other tools are not available to me.  I
only have the use of PHP.

So, any ideas of what I can do to generate some web stats?  Any tutorials?  
Free code?  I've been looking, and I'm not finding a whole lot of newbie
info in this area.


Re: Web Stats With PHP?

I'm not quite sure I understand exactly what you are trying to do. Do
you have access to the raw log files? It's very easy to convert the raw
log files into a csv. You may be able to simply replace all the spaces
with commas. If your stuck then give more details and I'll help you out.

Re: Web Stats With PHP?


Quoted text here. Click to load it

I do not have access to raw log files--that's why I want to create my own.  
Go Daddy does not provide them unless you pay an extra $1.95 per month.  If
I can easily do this on my own, why pay extra?

What I do have from my web host (Go Daddy) is a summary.  It'll tell me I
got 42 hits yesterday, but won't tell me the IPs those hits came from.  It
won't give times of the hits, or anything else of meaning.

What I get is a summary.  It is nothing meaningful.

I want raw log files.  I want to know that at 6:23 PM, someone from IP
xxx.xxx.xxx.xxx visited my site using IE 6, and was referred by Yahoo and
they are using a monitor with 800x600 resolution.

PHP is the only tool available to me with this hosting package.  So, I'm
hoping to create my own raw stats.

This cheapo hosting package is everything I need right now--except for the

So if anyone can offer any guideance regarding this, I'd appreciate it.

Thank you.

Re: Web Stats With PHP?

Bob II wrote:
Quoted text here. Click to load it


You won't be able to get accurate stats without the raw log files.  You
could put code at the start of each page to log information about that
page being loaded.  But it won't be a true representation of your
traffic (i.e. images being downloaded, etc.) - just that page.

I ran into this a few months ago.  Suddenly (over two days) I got about
a 20% increase in traffic for my favicon.ico.  Other pages didn't show
this increase.  I tracked it down to a site hotlinking to my
favicon.ico.  A quick change and this site's IP was barred from
accessing any of my pages.

But I would have never found it without the Apache logs.


To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
Member of Independent Computer Consultants Association - www.icca.org

Re: Web Stats With PHP?

" Go Daddy does not provide [raw logs] unless you pay an extra $1.95
per month."

I'd pay an extra $2/month.

If for some odd reason you don't want to then the easiest way to log
all of your traffic would be some mod_rewrite magic. You'd have to
check if they give you mod_rewrite capability because a lot of
free/dirt cheap hosts don't.

There are some hosts at reasonable prices that give you access to a lot
more than go daddy appears to. You can get an $8/month account with
lunarpages.com for example. I've used them a few times and they aren't
too bad.

Re: Web Stats With PHP?

It's possible - look into the global $_SERVER variable and some other
global variables ... read the beginning of the PHP manual (before the
function descriptions start)

Re: Web Stats With PHP?

Quoted text here. Click to load it

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":


#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

$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.



Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
<http://www.phplabs.com/ | PHP scripts, webmaster resources

Re: Web Stats With PHP?

intoned upon the aether:

Quoted text here. Click to load it

Create a demo page and add the following:


This will print everything you have available to you ($_SERVER, $_ENV,
$_GET, and etcetera are in $GLOBALS).  Now if you want to go past
that, then you will need to use client side JavaScript or something
similar to pass to you some additional values (client screen
resolution, and other such).  But much of what you can get from
Javascript really only has value in aggregate anyway (statistics from
small samples are rarely representative).

Anyway, $GLOBALS and MySQL will get you started.

have fun,


"In the End, we will remember not the words of our enemies,
 but the silence of our friends."

- Martin Luther King Jr. (1929-1968)

Photo Archive @ http://www.tearnet.com/Sean
Last Updated 29 Sept. 2004

Re: Web Stats With PHP?

Bob II wrote:
Quoted text here. Click to load it
I use bbclone.. http://bbclone.de

Have done for years.

Site Timeline