stats

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

stats

Post by Luke »

I'm just wondering if anybody knows where I can find a simple php program that will keep stats in a database if I upload it intp my website's main directory
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stats? what kind of stats?
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

this is where you could start:

Code: Select all

<?php

/* User definable variables */ 
$logfile =  "logfile.log";    /* Filename of log to write to */ 
$timezone =  "-0500";     /* Timezone correction */ 
$lookup_size = true;     /* Set to true to enable filesize lookup */ 
$document_root =  "logfile.log";

/* A note about the lookup_size directive:
* This will make this script lookup the size of the original file on disk,
* which may or may not be the same amount of data sent to the client.
* It does give you a hint though..
* Oh, you have to set $document_root aswell if this should work..
*/ 

function write_to_log($str) {
    if($fd = @fopen($GLOBALS[ "logfile"],  "a")) {
    fputs($fd, $str);
    fclose($fd);
    }
}

function get_var($name,$default) {
    if($var = getenv($name)) {
    return $var;
    } else {
    return $default;
    }
}

if($remote_host = get_var( "REMOTE_HOST", false)) {
    $remote_host = get_var( "REMOTE_ADDR",  "-");
}
$remote_user = get_var( "REMOTE_USER",  "-");
$remote_ident = get_var( "REMOTE_IDENT",  "-");
$server_port = get_var( "SERVER_PORT", 80);
if($server_port!=80) {
    $server_port =  ":" . $server_port;
} else {
    $server_port =  "";
}
$server_name = get_var( "SERVER_NAME",  "-");
$request_method = get_var( "REQUEST_METHOD",  "GET");
$request_uri = get_var( "REQUEST_URI",  "");
$user_agent = get_var( "HTTP_USER_AGENT",  "");
if($lookup_size == true && $document_root) {
    $filename = ereg_replace( "\?.*",  "", $request_uri);
    $filename =  "$document_root$filename";
    if(!$size = filesize($filename)) {
    $size = 0;
    }
} else {
    $size = 0;
}

$date = gmdate( "d/M/Y:H:i:s");
$log = "$remote_host $remote_ident $remote_user [$date $timezone] \"".
    "$request_method http://$server_name$server_port$request_uri\" 200 $size\n";

write_to_log($log);

?>

Include on every page, and you have a start to stats.
You will need to impliment a log file reader... i used 'analog'... there are a bunch, search google for log reader.
Post Reply