用 C 语言写的简单的 CGI 访问计数器程序,用文件保存的计数,我觉得比数据库高效一点吧,呵呵。
/* counter.c
* Heihaier
*/
#include
#include
int main(int argc, char * argv[])
{
unsigned long long int counter = 0;
FILE * f = NULL;
char file[256];
printf("Content-type: text/html;\n\n");
char * query = getenv("QUERY_STRING");
if(NULL == query)
goto error;
snprintf(file, 256, "./data/%s", query);
f = fopen(file, "rw+");
if(NULL == f)
goto error;
fread(&counter, sizeof(unsigned long long int), 1, f);
printf("document.write(\"%llu\");", ++counter);
fseek(f, 0, SEEK_SET);
fwrite(&counter, sizeof(unsigned long long int), 1, f);
fclose(f);
return 0;
error:
return -1;
}
gcc counter.c -o /var/web/cgi-bin/counter.cgi
mkdir /var/web/cgi-bin/data
touch /var/web/cgi-bin/data/www.heiher.info
chmod 666 /var/web/cgi-bin/data/www.heiher.info
Over!