A CGI for Access Statistics: Counter and Log program

in place July 11, 2000, last modified July 11, 2000


The following shows the source code of count and access log cgi in Unix web server (Apache web server in AIX). It uses two log files. One is the 'count.log' that contains the number of aceesses. The other is 'access.log' that contains accumulated access data including the number of access, access time (accumulated sec since 1970.1.1), acess time, a form parameter, host environment variable (remote host and remote add).


#include <stdio.h>
#include <time.h>
#include "typedef.h"

main()
{
int count;
entry entries[5];
int tloc;
int error_flag;
int vics_count=0;
char acc_date[25];

FILE *fp;
FILE *fpr;
FILE *fpw;

....
post_method(entries,&count);

fp = fopen("access.log", "a");

/* open the counter file and read the old one */
fpr = fopen("count.log", "r");
fscanf(fpr,"%d",&vics_count);

/* read current time(time()) and convert it to date format (ctime()) */
error_flag = time(&tloc);
strcpy(acc_date,ctime(&tloc));
acc_date[24]='\0';

/* incease the count and save the log info into access.log file */

vics_count = vics_count+1;
fprintf(fp,"%d|%d|%s",vics_count,tloc,acc_date);
fprintf(fp,"|%s|%s|%s|\n",entries[0].val,getenv("REMOTE_HOST"),getenv("REMOTE_ADDR"));

/* close the count file and write the increased count to the reopened count file. */
fclose(fpr);
fpw= fopen("count.log", "w");
fprintf(fpw,"%d",vics_count);

fclose(fp);
fclose(fpw);

}


Knowledge & Engineering Databases (c) copyright Namchul Do, 2000