SiteMinder / Webhosting
The Computer Merchant, Ltd
US-MA-North Quincy

Justtechjobs.com Post A Job | Post A Resume

Creating Your Own Log Files
The next problem I ran into was that I would get inacurate (or no) filesize specifications in the log because it's not possible to find out how much content that gets generated from your PHP script. I ended up recording the original size of the PHP3 file, as this would at least give me a hint about how much data is actually sent each day/week/month (it's an option - use it or don't).
Well, not much more to say except "Here's the code, use it!" Just include this file the first thing you do in all your PHP scripts.

<?php

/*
* Common Logfile Format script.
*
* Author: Mattias Nilsson <mattias_at_coolt.nu>
*
*/

/* User definable variables */
$logfile =  "clf.log";    /* Filename of log to write to */
$timezone =  "+0100";     /* Timezone correction */
$lookup_size = true;     /* Set to true to enable filesize lookup */
$document_root =  "/usr/local/apache/share/htdocs";

/* 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);

?>
--Mattias

[Page 1]  [Page 2]  


Comments:
creating log-in of activities using PHPGrey09/09/08 22:14
negate the if statement?Peter08/24/07 04:51
Java to php Sunny12/14/05 09:47
RE: how get book in pdf for PHPWes05/12/05 00:57
RE: HELP!Wes05/12/05 00:55
which tool i can use a log fileDeepak Se12/06/04 09:44
help!miaka11/12/02 05:20
RE: emailSwetha Balla10/28/02 23:46
what is phpabialsh 09/18/02 04:04
HELP!Nikki Tavasoli09/06/02 20:33
which tool i can use a log filesomeone09/06/02 18:56
Can I use PHPt o develop tools for internet MRUTAYISIRE PATRICK07/31/02 11:40
how get book in pdf for PHPeli07/09/02 15:35
Simple Logging??Chris McCarty06/07/02 17:36
RE: problem working?Catherine05/17/02 16:29
RE: Use this to log into a table:Catherine05/17/02 16:27
To view log file in webprasad04/09/02 08:04
? How to get this working....?Sili03/15/02 11:29
to view log file in webrosli03/09/02 00:14
RE: I have got this script working,, but....mika02/02/02 09:34
Uploading php fileVishal01/16/02 11:57
RE: can anyboday tell me how to make a login Joshua01/12/02 20:48
RE: can anyboday tell me how to make a login Maresal01/01/02 17:44
how to create set up fileskiran12/11/01 06:33
can anyboday tell me how to make a login fernanciso10/30/01 04:00
I have got this script working,, but....i.k10/19/01 15:45
Why $GLOBALS["logfile"] instead of $logfile oTrustle10/03/01 07:53
RE: emailFrank Polscheit09/18/01 10:27
RE: problem working?pavel09/14/01 09:06
Use this to log into a table:yves08/23/01 12:20
question!reza08/20/01 10:23
What log analyzer do you use?Katana07/09/01 11:24
Log AnalyzerKatana07/09/01 02:18
Thanks... but, recommend ...Jungyeon, Choe07/01/01 17:43
Generating log-file for 404Marcus Ohlsson06/19/01 07:21
RE: How to get referer URLkeykoder06/10/01 01:02
Logging Errors...JohnC06/07/01 18:36
RE: How to get referer URLAnders K. Madsen05/29/01 12:37
emailjosephine leone05/24/01 17:51
Very GoodGain05/24/01 08:24
How to get referer URLLuis Oliveira05/08/01 06:42
RE: Question about an errorPer04/21/01 18:09
RE: You should be able to calc. page output sizeEric Blade04/13/01 23:42
problem working?Conor04/09/01 11:23
Nice aricle.Dennis S.Davidoff03/15/01 08:34
RE: log file reader script???Frans03/08/01 20:04
log file reader script???John02/22/01 02:54
Thanks!Rufo01/26/01 02:32
You should be able to calc. page output sizeSkunk01/08/01 08:11
Question about an errorEagleEyes12/20/00 16:54
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.