|
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
| Comments: | ||
| creating log-in of activities using PHP | Grey | 09/09/08 22:14 |
| negate the if statement? | Peter | 08/24/07 04:51 |
| Java to php | Sunny | 12/14/05 09:47 |
| RE: how get book in pdf for PHP | Wes | 05/12/05 00:57 |
| RE: HELP! | Wes | 05/12/05 00:55 |
| which tool i can use a log file | Deepak Se | 12/06/04 09:44 |
| help! | miaka | 11/12/02 05:20 |
| RE: email | Swetha Balla | 10/28/02 23:46 |
| what is php | abialsh | 09/18/02 04:04 |
| HELP! | Nikki Tavasoli | 09/06/02 20:33 |
| which tool i can use a log file | someone | 09/06/02 18:56 |
| Can I use PHPt o develop tools for internet M | RUTAYISIRE PATRICK | 07/31/02 11:40 |
| how get book in pdf for PHP | eli | 07/09/02 15:35 |
| Simple Logging?? | Chris McCarty | 06/07/02 17:36 |
| RE: problem working? | Catherine | 05/17/02 16:29 |
| RE: Use this to log into a table: | Catherine | 05/17/02 16:27 |
| To view log file in web | prasad | 04/09/02 08:04 |
| ? How to get this working....? | Sili | 03/15/02 11:29 |
| to view log file in web | rosli | 03/09/02 00:14 |
| RE: I have got this script working,, but.... | mika | 02/02/02 09:34 |
| Uploading php file | Vishal | 01/16/02 11:57 |
| RE: can anyboday tell me how to make a login | Joshua | 01/12/02 20:48 |
| RE: can anyboday tell me how to make a login | Maresal | 01/01/02 17:44 |
| how to create set up files | kiran | 12/11/01 06:33 |
| can anyboday tell me how to make a login | fernanciso | 10/30/01 04:00 |
| I have got this script working,, but.... | i.k | 10/19/01 15:45 |
| Why $GLOBALS["logfile"] instead of $logfile o | Trustle | 10/03/01 07:53 |
| RE: email | Frank Polscheit | 09/18/01 10:27 |
| RE: problem working? | pavel | 09/14/01 09:06 |
| Use this to log into a table: | yves | 08/23/01 12:20 |
| question! | reza | 08/20/01 10:23 |
| What log analyzer do you use? | Katana | 07/09/01 11:24 |
| Log Analyzer | Katana | 07/09/01 02:18 |
| Thanks... but, recommend ... | Jungyeon, Choe | 07/01/01 17:43 |
| Generating log-file for 404 | Marcus Ohlsson | 06/19/01 07:21 |
| RE: How to get referer URL | keykoder | 06/10/01 01:02 |
| Logging Errors... | JohnC | 06/07/01 18:36 |
| RE: How to get referer URL | Anders K. Madsen | 05/29/01 12:37 |
| josephine leone | 05/24/01 17:51 | |
| Very Good | Gain | 05/24/01 08:24 |
| How to get referer URL | Luis Oliveira | 05/08/01 06:42 |
| RE: Question about an error | Per | 04/21/01 18:09 |
| RE: You should be able to calc. page output size | Eric Blade | 04/13/01 23:42 |
| problem working? | Conor | 04/09/01 11:23 |
| Nice aricle. | Dennis S.Davidoff | 03/15/01 08:34 |
| RE: log file reader script??? | Frans | 03/08/01 20:04 |
| log file reader script??? | John | 02/22/01 02:54 |
| Thanks! | Rufo | 01/26/01 02:32 |
| You should be able to calc. page output size | Skunk | 01/08/01 08:11 |
| Question about an error | EagleEyes | 12/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. | ||


