#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int get_cookie(char *cookiename) {
char *cookie, *tp;
int count = 0;
if((cookie = getenv("HTTP_COOKIE")) != NULL) {
tp = strtok(cookie, "= ");
while(tp != NULL) {
if(strcmp(tp, cookiename) == 0) {
tp = strtok(NULL, "; ");
break;
}
else {
tp = strtok(NULL, "; ");
}
if(tp != NULL) {
tp = strtok(NULL, "= ");
}
}
count = atoi(tp);
}
}
void set_cookie(char *cookiename, int cookielife, int count) {
time_t timer;
struct tm *date;
char expires[256];
timer = time(NULL);
timer += cookielife;
date = gmtime(&timer);
strftime(expires, 255, "%a, %d-%b-%Y %H:%M:%S GMT", date);
printf("Set-Cookie: %s=%d; expires=%s;\n", cookiename, count, expires);
}
int main(void) {
char cookiename[] = "count";
int cookielife = 50 * 60 * 24 * 30;
int count;
printf("Content-type: text/html\n");
count = get_cookie(cookiename);
count++;
set_cookie(cookiename, cookielife, count);
printf("\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>アクセス回数</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("アクセス:%d回目<br>\n", count);
printf("</body>\n");
printf("</html>\n");
return 0;
}
アクセスカウンターのフローチャートが知りたいです。教えてもらえれば助かります
フローチュート
Re: フローチュート
アクセスカウンターの仕様が分からないので答えようがありません。
- softya(ソフト屋)
- 副管理人
- 記事: 11677
- 登録日時: 15年前
- 住所: 東海地方
- 連絡を取る:
Re: フローチュート
これは、どちらかのサイトのコードでしょうか?
そのサイトのURLも教えて下さい。
そのサイトのURLも教えて下さい。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。
Re: フローチュート
フローチャートです。
int get_cookie(char *cookiename) { <------------------------------------------------+
char *cookie, *tp; |
int count = 0; |
|
if((cookie = getenv("HTTP_COOKIE")) != NULL) { |
tp = strtok(cookie, "= "); |
+--> while (tp != NULL) { |
| if (strcmp(tp, cookiename) == 0) { |
| tp = strtok(NULL, "; "); |
| break; ---------------------+ |
| } | |
| else { | |
| tp = strtok(NULL, "; "); | |
| } | |
| if (tp != NULL) { | |
| tp = strtok(NULL, "= "); | |
| } | |
+--- } | |
count = atoi(tp); <------------------+ |
} |
} ------------------------------------------------------------------------------->| |
| |
void set_cookie(char *cookiename, int cookielife, int count) { <---------------+ | |
time_t timer; | | |
struct tm *date; | | |
char expires[256]; | | |
| | |
timer = time(NULL); | | |
timer += cookielife; | | |
date = gmtime(&timer); | | |
| | |
strftime(expires, 255, "%a, %d-%b-%Y %H:%M:%S GMT", date); | | |
| | |
printf("Set-Cookie: %s=%d; expires=%s;\n", cookiename, count, expires); | | |
} ------------------------------------------------------------------>| | | |
| | | |
| | | |
( START ) | | | |
| | | | |
V | | | |
int main(void) { | | | |
char cookiename[] = "count"; | | | |
int cookielife = 50 * 60 * 24 * 30; | | | |
int count; | | | |
printf("Content-type: text/html\n"); | | | |
count = get_cookie(cookiename); --------------------------------|-------->| | |
count++; <-------------------------------+ | |
set_cookie(cookiename, cookielife, count); -----------------------------------|->|
printf("\n"); <---------------------------------------------+
printf("<html>\n");
printf("<head>\n");
printf("<title>アクセス回数</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("アクセス:%d回目<br>\n", count);
printf("</body>\n");
printf("</html>\n");
return 0;
}
|
V
( STOP )
Re: フローチュート
すみません。 get_cookie と set_cookie の呼び出しが逆でした。かずま さんが書きました:フローチャートです。
フローチャートというのは、左端を揃えて書く
「アセンブリ言語とか、昔の FORTRAN や BASIC のプログラム」
が読みにくいために存在するものです。
if や while で制御構造を表す言語では必要性の感じられないものです。
Re: フローチュート
勘違い投稿でした。
削除させていただきます。
削除させていただきます。
最後に編集したユーザー みけCAT on 2012年2月03日(金) 08:33 [ 編集 1 回目 ]
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)
Re: フローチュート
まだ質問者さんの返事が有りませんので,無視ではないと思います.みけCAT さんが書きました:これが無視されていますね。