実際にこれベースのものが、知人のサイトで動作しています。@@;
よそで書いたものの転載。初歩のC++でSSI includeで使うもの。Linux上のg++用に書いたが、他環境では動くかな?
C++でのロックの仕組みがいまいちよくわからず、昔ながらのファイルロックでごまかしている。まぁ初歩なので :D 参考にしたのは、LaTeXで有名な奥村研のC++入門。
////////////////////////////
//
// まったく初歩的なアクセスカウンター Ver 0.01a
// Date: 2008/10/11
// Author: *************
// License: GPL v2
// (とってもローパワーなサーバ用 with g++)
//
////////////////////////////
//
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <unistd.h>
#define COUNT "count.dat"
#define LOCKFILE "count.lock"
#define LOCKCOUNT 18
using namespace std;
int make_lock()
{
int i;
int r=-1;
for(i=0;i<LOCKCOUNT;i++){
ifstream cf(LOCKFILE);
if (!cf) {
r = 0;
ofstream gf(LOCKFILE);
gf << 1;
gf.close();
break;
}
sleep(1);
}
return(r);
}
void kill_lock()
{
remove(LOCKFILE);
}
int in_counter()
{
int n;
if (make_lock() == -1) {
n = -1;
} else {
ifstream f(COUNT);
if (f) {
f >> n;
f.close();
} else {
n = 0;
}
n++;
ofstream g(COUNT);
if (g) {
g << n;
g.close();
}
kill_lock();
}
return(n);
}
string aismain()
{
string a = "";
time_t t = time(0);
tm *x = localtime(&t);
if (x->tm_hour > 5 && x->tm_hour < 10) {
a += "おはよう";
} else if (x->tm_hour < 15) {
a +="こんにちは";
} else {
a +="こんばんは";
}
a += "。";
return(a);
}
int main()
{
int lf = in_counter();
const char* h = "Content-Type: text/html; charset=UTF-8\n\n";
cout << h << aismain();
if (lf == -1) {
cout << "ロックファイルエラーです!";
} else {
cout << "あなたは" << lf << "番めです";
}
cout << "\n";
}