Linux 版 (精华区)
发信人: netiscpu (网中鸟~~可以飞了), 信区: Linux
标 题: 用php实现站点访问数据的LOG和数据库储存
发信站: 哈工大紫丁香 (Fri Jul 9 07:32:34 1999), 转信
<?
/*
* Common Logfile Format script.
*
* Author: Mattias Nilsson <mattias@coolt.nu>
*
*/
/* User definable variables */
$logfile = "site.log"; /* Filename of log to write to */
$timezone = "+0800"; /* Timezone correction */
$lookup_size = true; /* Set to true to enable filesize lookup */
$document_root = "/home/apache/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..
*/
/* connect to mysql and write to a table*/
mysql_pconnect( "localhost", "root", "") or die( "Unable to connect to SQL
server");mysql_select_db( "internet") or die( "Unable to select database");
function insert_to_db($date,$useraddr,$useragent){
if($ret = mysql_query( "insert into accesslog values ('$date','$useradd
r','$useragent')")){ }else{
echo ( "Unable to insert into the accesslog table");
}
}
/*write to a log file*/
function write_to_log($str) {
if($fd = @fopen($GLOBALS[ "logfile"], "a")) {
fputs($fd, $str);
fclose($fd);
}
}
function read_log(){
if ($fd = @fopen($GLOBALS[ "logfile"], "r")){
$logs=fgets($fd,1024);
fclose($fd);
}
return ($logs);
}
function get_var($name,$default) {
if($var = getenv($name)) {
return $var;
} else {
return $default;
}
}
$remote_addr = get_var( "REMOTE_ADDR", "-");
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( "Y-M-D H:i:s");
$log = "$remote_addr $remote_host $remote_ident $remote_user [$date $time
zone] \"$request_method http://$server_name$server_port$request_uri\" $user
_agent 200 $size\n";write_to_log($log);
insert_to_db(date( "Y-m-d H:i:s"),$remote_addr,$user_agent);
?>
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: bin@mtlab.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.462毫秒